home *** CD-ROM | disk | FTP | other *** search
/ Over 1,000 Windows 95 Programs / Over 1000 Windows 95 Programs (Microforum) (Disc 2).iso / odbc_set / instcat.sql < prev    next >
Text File  |  1995-08-07  |  138KB  |  4,621 lines

  1. /*
  2. **    INSTCAT.SQL
  3. **    Installs catalog stored procedures on the Microsoft SQL Server.
  4. **    Copyright 1992-1995, Microsoft Corp.    All rights reserved.
  5. */
  6.  
  7. /*
  8. NOTE:  you MUST change the last row inserted into spt_server_info
  9. to be version number of this file.    the convention is j.nn.bbb, where
  10. jj is the major version number ('6' now), nn is the minor version number
  11. ('00' now), and bbb is the build number.
  12. */
  13.  
  14. /****************************************************************************/
  15. /* This portion sets up the ability to perform all the functions in this    */
  16. /* script                                                                    */
  17. /****************************************************************************/
  18. use master
  19. go
  20. dump tran master with no_log
  21. go
  22.  
  23. if exists (select * from sysobjects
  24.        where name = 'sp_configure' and sysstat & 0xf = 4)
  25. begin
  26.     execute sp_configure 'update',1
  27. end
  28. reconfigure with override
  29. go
  30.  
  31. /*
  32. ** If old versions of tables exist, drop them.
  33. */
  34. if (exists (select * from sysobjects
  35.         where name = 'MSdatatype_info' and sysstat & 0xf = 3))
  36.     drop table MSdatatype_info
  37. go
  38. if (exists (select * from sysobjects
  39.         where name = 'MSdatatype_info_ext' and sysstat & 0xf = 3))
  40.     drop table MSdatatype_info_ext
  41. go
  42. if (exists (select * from sysobjects
  43.         where name = 'MStable_types' and sysstat & 0xf = 3))
  44.     drop table MStable_types
  45. go
  46. if (exists (select * from sysobjects
  47.         where name = 'MSserver_info' and sysstat & 0xf = 3))
  48.     drop table MSserver_info
  49. go
  50. if (exists (select * from sysobjects
  51.         where name = 'spt_table_types' and sysstat & 0xf = 3))
  52.     drop table spt_table_types    /* no longer used */
  53. go
  54.  
  55. /*
  56. ** If tables or procs already exist, drop them.
  57. */
  58.  
  59. if (exists (select * from sysobjects
  60.         where name = 'spt_datatype_info' and sysstat & 0xf = 3))
  61.     drop table spt_datatype_info
  62. go
  63. if (exists (select * from sysobjects
  64.         where name = 'spt_datatype_info_ext' and sysstat & 0xf = 3))
  65.     drop table spt_datatype_info_ext
  66. go
  67. if (exists (select * from sysobjects
  68.         where name = 'spt_server_info' and sysstat & 0xf = 3))
  69.     drop table spt_server_info
  70. go
  71. if (exists (select * from sysobjects
  72.         where name = 'sp_tables' and sysstat & 0xf = 4))
  73.     drop proc sp_tables
  74. go
  75. if (exists (select * from sysobjects
  76.         where name = 'sp_statistics' and sysstat & 0xf = 4))
  77.     drop proc sp_statistics
  78. go
  79. if (exists (select * from sysobjects
  80.         where name = 'sp_columns' and sysstat & 0xf = 4))
  81.     drop proc sp_columns
  82. go
  83. if (exists (select * from sysobjects
  84.         where name = 'sp_fkeys' and sysstat & 0xf = 4))
  85.     drop proc sp_fkeys
  86. go
  87. if (exists (select * from sysobjects
  88.         where name = 'sp_pkeys' and sysstat & 0xf = 4))
  89.     drop proc sp_pkeys
  90. dump tran master with no_log
  91. go
  92.  
  93. go
  94. if (exists (select * from sysobjects
  95.         where name = 'sp_stored_procedures' and sysstat & 0xf = 4))
  96.     drop proc sp_stored_procedures
  97. go
  98. if (exists (select * from sysobjects
  99.         where name = 'sp_sproc_columns' and sysstat & 0xf = 4))
  100.     drop proc sp_sproc_columns
  101. go
  102. if (exists (select * from sysobjects
  103.         where name = 'sp_table_privileges' and sysstat & 0xf = 4))
  104.     drop proc sp_table_privileges
  105. go
  106. if (exists (select * from sysobjects
  107.         where name = 'sp_column_privileges' and sysstat & 0xf = 4))
  108.     drop proc sp_column_privileges
  109. go
  110. if (exists (select * from sysobjects
  111.         where name = 'sp_server_info' and sysstat & 0xf = 4))
  112.     drop proc sp_server_info
  113. go
  114. if (exists (select * from sysobjects
  115.         where name = 'sp_datatype_info' and sysstat & 0xf = 4))
  116.     drop proc sp_datatype_info
  117. go
  118. if (exists (select * from sysobjects
  119.         where name = 'sp_special_columns' and sysstat & 0xf = 4))
  120.     drop proc sp_special_columns
  121. go
  122. if (exists (select * from sysobjects
  123.         where name = 'sp_databases' and sysstat & 0xf = 4))
  124.     drop proc sp_databases
  125. go
  126.  
  127. dump tran master with no_log
  128. go
  129.  
  130. print 'creating table spt_datatype_info_ext'
  131. go
  132. create table spt_datatype_info_ext (
  133.                 user_type        smallint    not null,
  134.                 create_params    varchar(32) null,
  135.                 auto_increment    smallint null)
  136. go
  137.  
  138. create unique clustered index datatypeinfoextclust on spt_datatype_info_ext(user_type,auto_increment)
  139. go
  140.  
  141. grant select on spt_datatype_info_ext to public
  142. go
  143.  
  144.  
  145. insert into spt_datatype_info_ext
  146.     /* CHAR      user_type, create_params, auto_increment */
  147.     values             (1,    'length' ,0)
  148.  
  149. insert into spt_datatype_info_ext
  150.     /* VARCHAR     user_type, create_params, auto_increment */
  151.     values             (2,    'max length' ,0)
  152.  
  153. insert into spt_datatype_info_ext
  154.     /* BINARY     user_type, create_params, auto_increment */
  155.     values             (3,    'length' ,0)
  156.  
  157. insert into spt_datatype_info_ext
  158.     /* VARBINARY user_type, create_params, auto_increment */
  159.     values             (4,    'max length' ,0)
  160.  
  161. if    (charindex('6.00', @@version) > 0)
  162. begin    /*    Add 6.0 data types */
  163.     insert into spt_datatype_info_ext
  164.         /* DECIMAL user_type, create_params, auto_increment */
  165.         values             (26,    'precision,scale' ,0)
  166.  
  167.     insert into spt_datatype_info_ext
  168.         /* NUMERIC user_type, create_params, auto_increment */
  169.         values             (25,    'precision,scale' ,0)
  170.  
  171.     insert into spt_datatype_info_ext
  172.         /* DECIMAL IDENTITY user_type, create_params, auto_increment */
  173.         values             (26,    'precision' ,1)
  174.  
  175.     insert into spt_datatype_info_ext
  176.         /* NUMERIC IDENTITY user_type, create_params, auto_increment */
  177.         values             (25,    'precision' ,1)
  178.  
  179. end
  180. else    /*    Pre 6.0 server, add SYSNAME create param */
  181.     begin
  182.         insert into spt_datatype_info_ext
  183.             /* SYSNAME     user_type, create_param, auto_increments */
  184.             values             (18,    'max length' ,0)
  185.  
  186.     end
  187. go
  188.  
  189. print 'creating table spt_datatype_info'
  190. go
  191. create table spt_datatype_info (
  192.                 ss_dtype           tinyint        not null,
  193.                 type_name          varchar(32)  not null,
  194.                 data_type          smallint     not null,
  195.                 data_precision     int          null,
  196.                 numeric_scale       smallint     null,    /* min scale if 6.0 */
  197.                 numeric_radix      smallint     null,
  198.                 length             int          null,
  199.                 literal_prefix     varchar(32)  null,
  200.                 literal_suffix     varchar(32)  null,
  201.                 create_params      varchar(32)  null,
  202.                 nullable           smallint     not null,
  203.                 case_sensitive     smallint     not null,
  204.                 searchable         smallint     not null,
  205.                 unsigned_attribute smallint     null,
  206.                 money              smallint     not null,
  207.                 auto_increment     smallint     null,
  208.                 local_type_name    varchar(128) null,
  209.                 aux                int            null,
  210.                 numdec               tinyint        not null)
  211. go
  212.  
  213. create unique clustered index datatypeinfoclust on spt_datatype_info(ss_dtype,auto_increment)
  214. go
  215.  
  216. /*
  217.     There is a complicated set of SQL used to deal with
  218.     the SQL Server Null data types (MONEYn, INTn, etc.)
  219.     ISNULL is the only conditional SQL Server function that can be used
  220.     to differentiate between these types depending on size.
  221.  
  222.     The aux column in the above table is used to differentiate
  223.     the null data types from the non-null types.
  224.  
  225.     The aux column contains NULL for the null data types and 0 or delta
  226.     to be added to length to get precision (money types) for the non-null
  227.     data types.
  228.  
  229.     The following SQL returns the contents of the aux column (0)
  230.     for the non-null data types and returns a variable non-zero
  231.     value for the null data types.
  232.  
  233.                                  " I   I I FFMMDD"
  234.                                  " 1   2 4 484848"
  235.     isnull(d.aux,ascii(substring('666AAA@@@CB??GG',
  236.     2*(d.ss_dtype%35+1)+2-8/c.length,1))-60)
  237.  
  238.     The '2*(d.ss_dtype%35+1)+2-8/c.length' selects a specific character of
  239.     the substring mask depending on the null data type and its size, i.e.
  240.     null MONEY4 or null MONEY8.  The character selected is then converted
  241.     to its binary value and an appropriate bias (i.e. 60) is subtracted to
  242.     return the correct non-zero value.    This value may be used as a
  243.     constant, i.e. ODBC data type, precision, scale, etc., or used as an
  244.     index with a substring to pick out a character string, i.e. type name.
  245.  
  246.     The comments above the substring mask denote which character is
  247.     selected for each null data type, i.e. In (INTn), Fn (FLOATn),
  248.     Mn (MONEYn) and Dn (DATETIMn).
  249. */
  250.  
  251. grant select on spt_datatype_info to public
  252. go
  253.  
  254. /* Get case sensitivity */
  255. if 'A' = 'A' /* create dummy begin block */
  256. begin
  257.     declare @case smallint
  258.  
  259.     select @case = 0
  260.     select @case = 1 where 'a' != 'A'
  261.  
  262.     /* Local Binary */
  263.     insert into spt_datatype_info values
  264.     (  45      ,'binary',-2         ,null,null ,null,null,'0x',null,'length',1  ,0   ,2   ,null    ,0      ,null,'binary',0,0)
  265.  
  266.     /* Local Bit */
  267.     insert into spt_datatype_info values
  268.     (  50      ,'bit',-7       ,    1    ,0      ,2  ,null,null,null,null,0  ,0   ,2    ,null     ,0    ,null,'bit',0,0)
  269.  
  270.     /* Local Char */
  271.     insert into spt_datatype_info values
  272.     (  47      ,'char',1        ,null,null ,null,null,'''','''','length',1  ,@case,3    ,null     ,0    ,null,'char',0,0)
  273.  
  274.     /* Local Datetime */
  275.     insert into spt_datatype_info values
  276.     (  61      ,'datetime',11       ,23    ,3      ,10 ,16 ,'''','''',null,1    ,0    ,3     ,null      ,0    ,null,'datetime',0,0)
  277.  
  278.     /* Local Smalldatetime */
  279.     insert into spt_datatype_info values
  280.     (  58      ,'smalldatetime',11        ,16  ,0    ,10 ,16 ,'''','''',null,1    ,0     ,3   ,null    ,0     ,null,'smalldatetime',0,0)
  281.  
  282.     /* Local Datetimn */
  283.     insert into spt_datatype_info values /* sql server type is 'datetimn' */
  284.     (  111      ,'smalldatetime',0        ,0     ,0    ,10 ,0  ,'''','''',null,1    ,0     ,3   ,null    ,0     ,null,'datetime',null,0)
  285.  
  286.     /* Local Float */
  287.     insert into spt_datatype_info values
  288.     (  62      ,'float',6        ,15  ,null ,10 ,null,null,null,null,1  ,0    ,2     ,0       ,0    ,0     ,'float',0,0)
  289.  
  290.     /* Local RealFloat */
  291.     insert into spt_datatype_info values /* sql server type is 'floatn' */
  292.     (  109      ,'float        real',0        ,0     ,null ,10 ,0  ,null,null,null,1  ,0   ,2    ,0         ,0    ,0    ,'real      float',null,0)
  293.  
  294.     /* Local Real */
  295.     insert into spt_datatype_info values
  296.     (  59      ,'real',7        ,7    ,null ,10 ,null,null,null,null,1  ,0   ,2    ,0         ,0    ,0    ,'real',0,0)
  297.  
  298.     /* Local Smallmoney */
  299.     insert into spt_datatype_info values
  300.     (  122      ,'smallmoney',3         ,10  ,4    ,10 ,null,'$',null,null,1  ,0    ,2     ,0       ,1    ,0     ,'smallmoney',8,0)
  301.  
  302.     /* Local Int */
  303.     insert into spt_datatype_info values
  304.     (  56      ,'int',4          ,10  ,0     ,10 ,null,null,null,null,1  ,0   ,2   ,0        ,0      ,0   ,'int',0,0)
  305.  
  306.     /* Local Intn */
  307.     insert into spt_datatype_info values /* sql server type is 'intn' */
  308.     (  38      ,'smallint     tinyint',0          ,0   ,0     ,10 ,0  ,null,null,null,1    ,0     ,2   ,0       ,0     ,0   ,'tinyint   smallint',null,0)
  309.  
  310.     /* Local Money */
  311.     insert into spt_datatype_info values
  312.     (  60      ,'money',3        ,19  ,4    ,10 ,null,'$',null,null,1  ,0   ,2    ,0         ,1    ,0    ,'money',13,0)
  313.  
  314.     /* Local Moneyn */
  315.     insert into spt_datatype_info values    /* sql server type is 'moneyn' */
  316.     (  110      ,'smallmoney',0         ,0   ,4    ,10 ,0    ,'$',null,null,1  ,0   ,2    ,0         ,1    ,0    ,'smallmoneymoney',null,0)
  317.  
  318.     /* Local Smallint */
  319.     insert into spt_datatype_info values
  320.     (  52      ,'smallint',5        ,5    ,0      ,10 ,null,null,null,null,1  ,0   ,2    ,0         ,0    ,0    ,'smallint',0,0)
  321.  
  322.     /* Local Text */
  323.     insert into spt_datatype_info values
  324.     (  35      ,'text',-1       ,2147483647,null ,null,2147483647,'''','''',null,1  ,@case,1     ,null      ,0    ,null,'text',0,0)
  325.  
  326.     /* Local Varbinary */
  327.     insert into spt_datatype_info values
  328.     (  37      ,'varbinary',-3        ,null,null ,null,null,'0x',null,'max length',1    ,0     ,2   ,null    ,0     ,null,'varbinary',0,0)
  329.  
  330.     /* Local Tinyint */
  331.     insert into spt_datatype_info values
  332.     (  48      ,'tinyint',-6       ,3   ,0     ,10 ,null,null,null,null,1  ,0   ,2   ,1        ,0      ,0   ,'tinyint',0,0)
  333.  
  334.     /* Local Varchar */
  335.     insert into spt_datatype_info values
  336.     (  39      ,'varchar',12       ,null,null ,null,null,'''','''','max length',1    ,@case,3   ,null    ,0      ,null,'varchar',0,0)
  337.  
  338.     /* Local Image */
  339.     insert into spt_datatype_info values
  340.     (  34      ,'image',-4        ,2147483647,null ,null,2147483647,'0x',null,null,1    ,0     ,0     ,null      ,0     ,null,'image',0,0)
  341.  
  342.     if    (charindex('6.00', @@version) > 0)
  343.     begin    /*    Add 6.0 data types */
  344.         /* Local Decimaln */
  345.         insert into spt_datatype_info values    /* sql server type is 'decimaln' */
  346.         (  106      ,'decimal',3       ,38    ,0     ,10 ,null,null,null,'precision,scale',1    ,0    ,2    ,0        ,0      ,0   ,'decimal',0,1)
  347.  
  348.         /* Local Numericn */
  349.         insert into spt_datatype_info values    /* sql server type is 'numericn' */
  350.         (  108      ,'numeric',2    ,38  ,0     ,10 ,null,null,null,'precision,scale',1     ,0  ,2  ,0      ,0    ,0    ,'numeric',0,1)
  351.  
  352.         /* Local Decimal */
  353.         insert into spt_datatype_info values    /* sql server type is 'decimaln' */
  354.         (  55      ,'decimal',3       ,38    ,0     ,10 ,null,null,null,'precision,scale',1    ,0    ,2    ,0        ,0      ,0   ,'decimal',0,1)
  355.  
  356.         /* Local Numeric */
  357.         insert into spt_datatype_info values    /* sql server type is 'numericn' */
  358.         (  63      ,'numeric',2    ,38  ,0     ,10 ,null,null,null,'precision,scale',1     ,0  ,2  ,0      ,0    ,0    ,'numeric',0,1)
  359.  
  360.         /* Identity attribute data types */
  361.  
  362.         /* Identity Int */
  363.         insert into spt_datatype_info values
  364.         (  56      ,'int identity',4          ,10  ,0     ,10 ,null,null,null,null,0    ,0     ,2   ,0        ,0      ,1   ,'int identity',0,0)
  365.  
  366.         /* Identity Smallint */
  367.         insert into spt_datatype_info values
  368.         (  52      ,'smallint identity',5       ,5    ,0      ,10 ,null,null,null,null,0  ,0   ,2    ,0         ,0    ,1    ,'smallint identity',0,0)
  369.  
  370.         /* Identity Tinyint */
  371.         insert into spt_datatype_info values
  372.         (  48      ,'tinyint identity',-6      ,3   ,0     ,10 ,null,null,null,null,0    ,0     ,2   ,1        ,0      ,1   ,'tinyint identity',0,0)
  373.  
  374.         /* Identity Decimaln */
  375.         insert into spt_datatype_info values    /* sql server type is 'decmaln' */
  376.         (  106      ,'decimal() identity',3       ,38    ,0     ,10 ,null,null,null,'precision,scale',0    ,0    ,2    ,0        ,0      ,1   ,'decimal() identity',0,1)
  377.  
  378.         /* Identity Numericn */
  379.         insert into spt_datatype_info values    /* sql server type is 'decmaln' */
  380.         (  108      ,'numeric() identity',2    ,38  ,0     ,10 ,null,null,null,'precision,scale',0     ,0  ,2  ,0      ,0    ,1    ,'numeric() identity',0,1)
  381.  
  382.         /* Identity Decimal */
  383.         insert into spt_datatype_info values    /* sql server type is 'decmaln' */
  384.         (  55      ,'decimal() identity',3       ,38    ,0     ,10 ,null,null,null,'precision,scale',0    ,0    ,2    ,0        ,0      ,1   ,'decimal() identity',0,1)
  385.  
  386.         /* Identity Numeric */
  387.         insert into spt_datatype_info values    /* sql server type is 'decmaln' */
  388.         (  63      ,'numeric() identity',2    ,38  ,0     ,10 ,null,null,null,'precision,scale',0     ,0  ,2  ,0      ,0    ,1    ,'numeric() identity',0,1)
  389.  
  390.     end
  391.     else    /*    Pre 6.0 server, remove money length adjustment */
  392.     begin
  393.         update spt_datatype_info set aux=0 where ss_dtype in (122,60)
  394.     end
  395. end
  396. go
  397.  
  398. dump tran master with no_log
  399. go
  400.  
  401. print 'creating table spt_server_info'
  402. go
  403. create table spt_server_info (
  404.               attribute_id        int NOT NULL,
  405.               attribute_name    varchar(60) NOT NULL,
  406.               attribute_value    varchar(255) NOT NULL)
  407. go
  408.  
  409. create unique clustered index serverinfoclust on spt_server_info(attribute_id)
  410. go
  411.  
  412. insert into spt_server_info
  413.     values (1,'DBMS_NAME','Microsoft SQL Server')
  414. insert into spt_server_info
  415.     values (2,'DBMS_VER',@@version)
  416. insert into spt_server_info
  417.     values (10,'OWNER_TERM','owner')
  418. insert into spt_server_info
  419.     values (11,'TABLE_TERM','table')
  420. insert into spt_server_info
  421.     values (12,'MAX_OWNER_NAME_LENGTH','30')
  422. insert into spt_server_info
  423.     values (13,'TABLE_LENGTH','30')
  424. insert into spt_server_info
  425.     values (14,'MAX_QUAL_LENGTH','30')
  426. insert into spt_server_info
  427.     values (15,'COLUMN_LENGTH','30')
  428. insert into spt_server_info
  429.     values (16,'IDENTIFIER_CASE','MIXED')
  430. insert into spt_server_info
  431.     values (17,'TX_ISOLATION','2')
  432. if    (charindex('6.00', @@version) > 0)
  433. begin    /*    Add 6.0 collation sequence */
  434.     insert into spt_server_info
  435.         select 18,'COLLATION_SEQ',
  436.             'charset='+t2.name+' sort_order='+t1.name
  437.             +' charset_num='+rtrim(convert(char(4),t1.csid))+
  438.             ' sort_order_num='+rtrim(convert(char(4),t1.id))
  439.         from syscharsets t1, syscharsets t2, sysconfigures t3
  440.         where t1.csid=t2.id and t1.id=t3.value and t3.config=1123
  441. end
  442. else
  443. begin    /*    Add 4.2x collation sequence */
  444.     insert into spt_server_info
  445.         select 18,"COLLATION_SEQ",
  446.             "charset="+t2.name+" sort_order="+t1.name
  447.             +" charset_num="+rtrim(convert(char(4),t1.csid))+
  448.             " sort_order_num="+rtrim(convert(char(4),t1.id))
  449.         from syscharsets t1, syscharsets t2, sysconfigures t3
  450.         where t1.csid=t2.id and t1.id=t3.value and t3.config=123
  451. end
  452. insert into spt_server_info
  453.     values (19,'SAVEPOINT_SUPPORT','Y')
  454. insert into spt_server_info
  455.     values (20,'MULTI_RESULT_SETS','Y')
  456. insert into spt_server_info
  457.     values (22,'ACCESSIBLE_TABLES','Y')
  458. insert into spt_server_info
  459.     values (100,'USERID_LENGTH','30')
  460. insert into spt_server_info
  461.     values (101,'QUALIFIER_TERM','database')
  462. insert into spt_server_info
  463.     values (102,'NAMED_TRANSACTIONS','Y')
  464. insert into spt_server_info
  465.     values (103,'SPROC_AS_LANGUAGE','Y')
  466. insert into spt_server_info
  467.     values (104,'ACCESSIBLE_SPROC','Y')
  468. insert into spt_server_info
  469.     values (105,'MAX_INDEX_COLS','16')
  470. insert into spt_server_info
  471.     values (106,'RENAME_TABLE','Y')
  472. insert into spt_server_info
  473.     values (107,'RENAME_COLUMN','Y')
  474. insert into spt_server_info
  475.     values (108,'DROP_COLUMN','N')
  476. insert into spt_server_info
  477.     values (109,'INCREASE_COLUMN_LENGTH','N')
  478. insert into spt_server_info
  479.     values (110,'DDL_IN_TRANSACTION','N')
  480. insert into spt_server_info
  481.     values (111,'DESCENDING_INDEXES','N')
  482. insert into spt_server_info
  483.     values (112,'SP_RENAME','Y')
  484. insert into spt_server_info
  485.     values (113,'REMOTE_SPROC','Y')
  486. insert into spt_server_info
  487.     values (500,'SYS_SPROC_VERSION','6.00.120')
  488. go
  489.  
  490. grant select on spt_server_info to public
  491. go
  492.  
  493. print 'creating sp_tables'
  494. go
  495.  
  496. create procedure sp_tables(
  497.                @table_name        varchar(90)    = null,
  498.                @table_owner     varchar(90)    = null,
  499.                @table_qualifier varchar(90)    = null,
  500.                @table_type        varchar(100) = null)
  501. as
  502.     declare @type1 varchar(3)
  503.     declare @tableindex int
  504.  
  505.  
  506.     /* Special feature #1:    enumerate databases when owner and name
  507.          are blank but qualifier is explicitly '%'.  */
  508.     if @table_qualifier = '%' and
  509.         @table_owner = '' and
  510.         @table_name = ''
  511.     begin    /* If enumerating databases */
  512.         select
  513.             table_qualifier = name,
  514.             table_owner = convert(varchar(32),null),
  515.             table_name = convert(varchar(32),null),
  516.             table_type = convert(varchar(12),null),
  517.             remarks = convert(varchar(254),null)    /* Remarks are NULL */
  518.         from master..sysdatabases
  519.         where name != 'model'    /* eliminate MODEL database */
  520.         order by table_qualifier
  521.     end
  522.  
  523.     /* Special feature #2:    enumerate owners when qualifier and name
  524.          are blank but owner is explicitly '%'.  */
  525.     else if @table_qualifier = '' and
  526.         @table_owner = '%' and
  527.         @table_name = ''
  528.     begin    /* If enumerating owners */
  529.         select distinct
  530.             table_qualifier = convert(varchar(32),null),
  531.             table_owner = user_name(uid),
  532.             table_name = convert(varchar(32),null),
  533.             table_type = convert(varchar(12),null),
  534.             remarks = convert(varchar(254),null)    /* Remarks are NULL */
  535.         from sysobjects
  536.         order by table_owner
  537.     end
  538.  
  539.     /* Special feature #3:    enumerate table types when qualifier, owner and
  540.          name are blank but table type is explicitly '%'.    */
  541.     else if @table_qualifier = '' and
  542.         @table_owner = '' and
  543.         @table_name = '' and
  544.         @table_type = '%'
  545.     begin    /* If enumerating table types */
  546.         select
  547.             table_qualifier = convert(varchar(32),null),
  548.             table_owner = convert(varchar(32),null),
  549.             table_name = convert(varchar(32),null),
  550.             table_type = rtrim(substring('SYSTEM TABLETABLE       VIEW',(colid-1)*12+1,12)),
  551.             remarks = convert(varchar(254),null)    /* Remarks are NULL */
  552.         from sysobjects o, syscolumns c
  553.         where o.id=c.id and o.name='sysusers' and colid<=3
  554.     end
  555.  
  556.     else
  557.     begin /* end of special features -- do normal processing */
  558.         if @table_qualifier is not null
  559.         begin
  560.             if db_name() != @table_qualifier
  561.             begin
  562.                 if @table_qualifier = ''
  563.                 begin  /* If empty qualifier supplied */
  564.                     /* Force an empty result set */
  565.                     select @table_name = ''
  566.                     select @table_owner = ''
  567.                 end
  568.                 else
  569.                 begin    /* If qualifier doesn't match current database */
  570.                     raiserror 20001 'Table qualifier must be name of current database'
  571.                     return
  572.                 end
  573.             end
  574.         end
  575.         if @table_type is null
  576.         begin    /* Select all ODBC supported table types */
  577.             select @type1 = 'SUV'
  578.         end
  579.         else
  580.         begin
  581.             /*    TableType is case sensitive if CS server */
  582.             select @type1 = null
  583.             if (charindex('''SYSTEM TABLE''',@table_type) != 0)
  584.                 select @type1 = @type1 + 'S'    /* Add System Tables */
  585.             if (charindex('''TABLE''',@table_type) != 0)
  586.                 select @type1 = @type1 + 'U'    /* Add User Tables */
  587.             if (charindex('''VIEW''',@table_type) != 0)
  588.                 select @type1 = @type1 + 'V'    /* Add Views */
  589.         end
  590.         if @table_name is null
  591.         begin    /*    If table name not supplied, match all */
  592.             select @table_name = '%'
  593.         end
  594.         else
  595.         begin
  596.             if (@table_owner is null) and (charindex('%', @table_name) = 0)
  597.             begin    /* If owner not specified and table is specified */
  598.                 if exists (select * from sysobjects
  599.                     where uid = user_id()
  600.                     and name = @table_name
  601.                     and (type = 'U' or type = 'V' or type = 'S'))
  602.                 begin    /* Override supplied owner w/owner of table */
  603.                     select @table_owner = user_name()
  604.                 end
  605.             end
  606.         end
  607.         if @table_owner is null /* If no owner supplied, force wildcard */
  608.             select @table_owner = '%'
  609.         select
  610.             table_qualifier = db_name(),
  611.             table_owner = user_name(o.uid),
  612.             table_name = convert(varchar(32),o.name),    /* make nullable */
  613.             table_type = rtrim(
  614.                 substring('SYSTEM TABLE            TABLE       VIEW       ',
  615.                     (ascii(o.type)-83)*12+1,12)),    /* 'S'=0,'U'=2,'V'=3 */
  616.             remarks = convert(varchar(254),null)    /* Remarks are NULL */
  617.         from sysusers u, sysobjects o
  618.         where
  619.             o.name like @table_name
  620.             and user_name(o.uid) like @table_owner
  621.             and charindex(substring(o.type,1,1),@type1)! = 0 /* Only desired types */
  622.             and u.uid = user_id() /* constrain sysusers uid for use in subquery */
  623.             and (
  624.                 suser_id() = 1     /* User is the System Administrator */
  625.                 or o.uid = user_id()     /* User created the object */
  626.                 /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  627.                 or ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  628.                     from sysprotects p
  629.                     /* outer join to correlate with all rows in sysobjects */
  630.                     where p.id =* o.id
  631.                         /* get rows for public,current user,user's group */
  632.                         and (p.uid = 0 or p.uid = user_id() or p.uid =* u.gid)
  633.                         /* check for SELECT,EXECUTE privilege */
  634.                         and (action in (193,224)))&1     /* more magic...normalize GRANT */
  635.                     ) = 1    /* final magic...compare Grants      */
  636.             )
  637.         order by table_type, table_qualifier, table_owner, table_name
  638.     end
  639. go
  640.  
  641. grant execute on sp_tables to public
  642. go
  643.  
  644. dump tran master with no_log
  645. go
  646.  
  647. print 'creating sp_statistics'
  648. go
  649.  
  650. CREATE PROCEDURE sp_statistics (
  651.                  @table_name        varchar(32),
  652.                  @table_owner        varchar(32) = null,
  653.                  @table_qualifier    varchar(32) = null,
  654.                  @index_name        varchar(32) = '%',
  655.                  @is_unique         char(1) = 'N',
  656.                  @accuracy            char(1) = 'Q')
  657. AS
  658.     DECLARE @indid                int
  659.     DECLARE @lastindid            int
  660.     DECLARE @table_id            int
  661.     DECLARE @full_table_name    char(70)
  662.  
  663.     if @table_qualifier is not null
  664.     begin
  665.         if db_name() != @table_qualifier
  666.         begin    /* If qualifier doesn't match current database */
  667.             raiserror 20001 'Table qualifier must be name of current database'
  668.             return
  669.         end
  670.     end
  671.  
  672.     if @accuracy not in ('Q','E')
  673.         begin
  674.             raiserror 20002 'Illegal ''accuracy'' specified -- must be ''Q'' or ''E''.'
  675.             return
  676.         end
  677.  
  678.     if @@trancount != 0
  679.     begin    /* If inside a transaction */
  680.         raiserror 20003 'The procedure ''sp_statistics'' cannot be executed from within a transaction.'
  681.         return
  682.     end
  683.     create table #TmpIndex(
  684.                 table_qualifier varchar(32) NULL,
  685.                 table_owner     varchar(32) NULL,
  686.                 table_name        varchar(32) NOT NULL,
  687.                 index_qualifier varchar(32) null,
  688.                 index_name      varchar(32) null,
  689.                 non_unique      smallint null,
  690.                 type            smallint NOT NULL,
  691.                 seq_in_index    smallint null,
  692.                 column_name     varchar(32) null,
  693.                 collation       char(1) null,
  694.                 index_id        int null,
  695.                 cardinality     int null,
  696.                 pages           int null,
  697.                 status            smallint NOT NULL)
  698.     if @table_owner is null
  699.     begin    /* If unqualified table name */
  700.         SELECT @full_table_name = @table_name
  701.     end
  702.     else
  703.     begin    /* Qualified table name */
  704.         SELECT @full_table_name = @table_owner + '.' + @table_name
  705.     end
  706.     /*    Get Object ID */
  707.     SELECT @table_id = object_id(@full_table_name)
  708.  
  709.     /*    Start at lowest index id */
  710.     SELECT @indid = min(indid)
  711.     FROM sysindexes
  712.     WHERE id = @table_id
  713.         AND indid > 0
  714.         AND indid < 255
  715.  
  716.     WHILE @indid != NULL
  717.     BEGIN
  718.         INSERT #TmpIndex    /* Add all columns that are in index */
  719.             SELECT
  720.                 DB_NAME(),                                /* table_qualifier */
  721.                 USER_NAME(o.uid),                        /* table_owner       */
  722.                 o.name,                                 /* table_name       */
  723.                 o.name,                                 /* index_qualifier */
  724.                 x.name,                                 /* index_name       */
  725.                 0,                                        /* non_unique       */
  726.                 1,                                        /* SQL_INDEX_CLUSTERED */
  727.                 colid,                                    /* seq_in_index    */
  728.                 INDEX_COL(@full_table_name,indid,colid),/* column_name       */
  729.                 'A',                                    /* collation       */
  730.                 @indid,                                 /* index_id        */
  731.                 x.rows,                                 /* cardinality       */
  732.                 x.dpages,                                /* pages           */
  733.                 x.status                                /* status            */
  734.             FROM sysindexes x, syscolumns c, sysobjects o
  735.             WHERE
  736.                 x.id = @table_id
  737.                 AND x.id = o.id
  738.                 AND x.id = c.id
  739.                 AND c.colid < keycnt+(x.status&16)/16
  740.                 AND x.indid = @indid
  741.         /*
  742.         **      Now move @indid to the next index.
  743.         */
  744.         SELECT @lastindid = @indid
  745.         SELECT @indid = NULL
  746.  
  747.         SELECT @indid = min(indid)
  748.         FROM sysindexes
  749.         WHERE id = @table_id
  750.             AND indid > @lastindid
  751.             AND indid < 255
  752.     END
  753.  
  754.     UPDATE #TmpIndex
  755.         SET non_unique = 1
  756.         WHERE status&2 != 2 /* If non-unique index */
  757.     UPDATE #TmpIndex
  758.         SET
  759.             type = 3,            /* SQL_INDEX_OTHER */
  760.             cardinality = NULL,
  761.             pages = NULL
  762.         WHERE index_id > 1    /* If non-clustered index */
  763.  
  764.     /* now add row for table statistics */
  765.     INSERT #TmpIndex
  766.         SELECT
  767.             DB_NAME(),                /* table_qualifier */
  768.             USER_NAME(o.uid),        /* table_owner       */
  769.             o.name,                 /* table_name       */
  770.             null,                    /* index_qualifier */
  771.             null,                    /* index_name       */
  772.             null,                    /* non_unique       */
  773.             0,                        /* SQL_TABLE_STAT  */
  774.             null,                    /* seq_in_index    */
  775.             null,                    /* column_name       */
  776.             null,                    /* collation       */
  777.             0,                        /* index_id        */
  778.             x.rows,                 /* cardinality       */
  779.             x.dpages,                /* pages           */
  780.             0                        /* status           */
  781.         FROM sysindexes x, sysobjects o
  782.         WHERE o.id = @table_id
  783.             AND x.id = o.id
  784.             AND (x.indid = 0 or x.indid = 1)    /*    If there are no indexes */
  785.                                                 /*    then table stats are in */
  786.                                                 /*    a row with indid =0        */
  787.  
  788.     if @is_unique != 'Y'    /* If all indexes desired */
  789.         SELECT
  790.             table_qualifier,
  791.             table_owner,
  792.             table_name,
  793.             non_unique,
  794.             index_qualifier,
  795.             index_name,
  796.             type,
  797.             seq_in_index,
  798.             column_name,
  799.             collation,
  800.             cardinality,
  801.             pages,
  802.             filter_condition = convert(varchar(128),null)
  803.         FROM #TmpIndex
  804.         WHERE
  805.             index_name like @index_name /* If matching name */
  806.             or index_name is null        /* If SQL_TABLE_STAT row */
  807.         ORDER BY non_unique, type, index_name, seq_in_index
  808.     else                    /* If only unique indexes desired */
  809.         SELECT
  810.             table_qualifier,
  811.             table_owner,
  812.             table_name,
  813.             non_unique,
  814.             index_qualifier,
  815.             index_name,
  816.             type,
  817.             seq_in_index,
  818.             column_name,
  819.             collation,
  820.             cardinality,
  821.             pages,
  822.             filter_condition = convert(varchar(128),null)
  823.         FROM #TmpIndex
  824.         WHERE
  825.             (non_unique = 0             /* If unique */
  826.                 or non_unique is NULL)    /* If SQL_TABLE_STAT row */
  827.             and (index_name like @index_name    /* If matching name */
  828.                 or index_name is null)    /* If SQL_TABLE_STAT row */
  829.         ORDER BY non_unique, type, index_name, seq_in_index
  830.  
  831.     DROP TABLE #TmpIndex
  832. go
  833.  
  834. grant execute on sp_statistics to public
  835. go
  836.  
  837. dump tran master with no_log
  838. go
  839.  
  840.  
  841. print 'creating sp_columns'
  842. go
  843.  
  844. /*    Procedure for pre-6.0 server */
  845. CREATE PROCEDURE sp_columns (
  846.                  @table_name        varchar(90),
  847.                  @table_owner        varchar(90) = null,
  848.                  @table_qualifier    varchar(90) = null,
  849.                  @column_name        varchar(90) = null )
  850. AS
  851.     DECLARE @full_table_name    char(181)
  852.     DECLARE @table_id int
  853.  
  854.     if @column_name is null /*    If column name not supplied, match all */
  855.         select @column_name = '%'
  856.     if @table_qualifier is not null
  857.     begin
  858.         if db_name() != @table_qualifier
  859.         begin    /* If qualifier doesn't match current database */
  860.             raiserror 20001 'Table qualifier must be name of current database'
  861.             return
  862.         end
  863.     end
  864.     if @table_name is null
  865.     begin    /*    If table name not supplied, match all */
  866.         select @table_name = '%'
  867.     end
  868.     if @table_owner is null
  869.     begin    /* If unqualified table name */
  870.         SELECT @full_table_name = @table_name
  871.     end
  872.     else
  873.     begin    /* Qualified table name */
  874.         SELECT @full_table_name = @table_owner + '.' + @table_name
  875.     end
  876.  
  877.     /*    Get Object ID */
  878.     SELECT @table_id = object_id(@full_table_name)
  879.     if ((charindex('%',@full_table_name) = 0) and
  880.         (charindex('_',@full_table_name) = 0)  and
  881.         @table_id != 0)
  882.     begin
  883.         /* this block is for the case where there is no pattern
  884.              matching required for the table name */
  885.         SELECT    /* INTn, FLOATn, DATETIMEn and MONEYn types */
  886.             table_qualifier = DB_NAME(),
  887.             table_owner = USER_NAME(o.uid),
  888.             table_name = o.name,
  889.             column_name = c.name,
  890.             data_type = d.data_type+convert(smallint,
  891.                         /*                " I   I I FFMMDD" */
  892.                         /*                " 1   2 4 484848" */
  893.                         ascii(substring('666AAA@@@CB??GG',
  894.                         2*(d.ss_dtype%35+1)+2-8/c.length,1))
  895.                         -60),
  896.             type_name = rtrim(substring(d.type_name,
  897.                         1+
  898.                         /*                " I   I I FFMMDD" */
  899.                         /*                " 1   2 4 484848" */
  900.                         ascii(substring('III<<<MMMI<<A<A',
  901.                         2*(d.ss_dtype%35+1)+2-8/c.length,
  902.                         1))-60, 13)),
  903.             "precision" = d.data_precision +convert(int,
  904.                         /*                " I   I I FFMMDD" */
  905.                         /*                " 1   2 4 484848" */
  906.                         ascii(substring('???AAAFFFCKFOLS',
  907.                         2*(d.ss_dtype%35+1)+2-8/c.length,1))-60),
  908.             length = d.length +convert(int,
  909.                         /*                " I   I I FFMMDD" */
  910.                         /*                " 1   2 4 484848" */
  911.                         ascii(substring('AAA<BB<DDDHLUPP',
  912.                         2*(d.ss_dtype%35+1)+2-8/c.length,
  913.                         1))-64),
  914.             scale = d.numeric_scale +convert(smallint,
  915.                         /*                " I   I I FFMMDD" */
  916.                         /*                " 1   2 4 484848" */
  917.                         ascii(substring('<<<<<<<<<<<<<<?',
  918.                         2*(d.ss_dtype%35+1)+2-8/c.length,
  919.                         1))-60),
  920.             radix = d.numeric_radix,
  921.             nullable = convert(smallint, 1),
  922.             remarks = convert(varchar(254),null),    /* Remarks are NULL */
  923.             ss_data_type = c.type,
  924.             colid = c.colid
  925.         FROM
  926.             syscolumns c,
  927.             sysobjects o,
  928.             master.dbo.spt_datatype_info d
  929.         WHERE
  930.             o.id = @table_id
  931.             AND c.id = o.id
  932.             AND c.type = d.ss_dtype
  933.             AND o.type != 'P'
  934.             AND c.name like @column_name
  935.             AND d.ss_dtype IN (111, 109, 38, 110)    /* Just *N types */
  936.             AND c.usertype < 100                    /* No user defined types */
  937.         UNION ALL
  938.         SELECT    /* All other types including user data types */
  939.             table_qualifier = DB_NAME(),
  940.             table_owner = USER_NAME(o.uid),
  941.             table_name = o.name,
  942.             column_name = c.name,
  943.             data_type = convert(smallint,
  944.                 /*    Map systypes.type to ODBC type */
  945.                 /*        SS-Type         "     1 1             1           "*/
  946.                 /*                        "33 3030    4 44 5 5 255 556666"*/
  947.                 /*                        "45 7698    5 78 0 2 256 890123"*/
  948.                         ascii(substring('8;<9?H><<<<:<=6<5<A<??@<GC?GB>',
  949.                         t.type%34+1,1))-60),
  950.             type_name = t.name,
  951.             "precision" = isnull(d.data_precision, convert(int,c.length))
  952.                         +isnull(d.aux, convert(int,
  953.                         /*                " I   I I FFMMDD" */
  954.                         /*                " 1   2 4 484848" */
  955.                         ascii(substring('???AAAFFFCKFOLS',
  956.                         2*(d.ss_dtype%35+1)+2-8/c.length,1))-60)),
  957.             length = isnull(d.length, convert(int,c.length)) +convert(int,
  958.                         isnull(d.aux,
  959.                         /*                " I   I I FFMMDD" */
  960.                         /*                " 1   2 4 484848" */
  961.                         ascii(substring('AAA<BB<DDDHLUPP',
  962.                         2*(d.ss_dtype%35+1)+2-8/c.length,
  963.                         1))-64)),
  964.             scale = d.numeric_scale +convert(smallint,
  965.                         isnull(d.aux,
  966.                         /*                " I   I I FFMMDD" */
  967.                         /*                " 1   2 4 484848" */
  968.                         ascii(substring('<<<<<<<<<<<<<<?',
  969.                         2*(d.ss_dtype%35+1)+2-8/c.length,
  970.                         1))-60)),
  971.             radix = d.numeric_radix,
  972.             nullable =    /* set nullability from status flag */
  973.                 convert(smallint, convert(bit, c.status&8)),
  974.             remarks = convert(varchar(254),null),    /* Remarks are NULL */
  975.             ss_data_type = c.type,
  976.             colid = c.colid
  977.         FROM
  978.             syscolumns c,
  979.             sysobjects o,
  980.             master.dbo.spt_datatype_info d,
  981.             systypes t
  982.         WHERE
  983.             o.id = @table_id
  984.             AND c.id = o.id
  985.             AND c.type = d.ss_dtype
  986.             AND o.type != 'P'
  987.             AND c.usertype *= t.usertype
  988.             AND c.name like @column_name
  989.             AND (d.ss_dtype NOT IN (111, 109, 38, 110)    /* No *N types */
  990.                 OR c.usertype >= 100)                    /* User defined types */
  991.         ORDER BY colid
  992.     end
  993.     else
  994.     begin
  995.         /* this block is for the case where there IS pattern
  996.              matching done on the table name */
  997.         if @table_owner is null /*    If owner not supplied, match all */
  998.             select @table_owner = '%'
  999.         SELECT    /* INTn, FLOATn, DATETIMEn and MONEYn types */
  1000.             table_qualifier = DB_NAME(),
  1001.             table_owner = USER_NAME(o.uid),
  1002.             table_name = o.name,
  1003.             column_name = c.name,
  1004.             data_type = d.data_type+convert(smallint,
  1005.                         /*                " I   I I FFMMDD" */
  1006.                         /*                " 1   2 4 484848" */
  1007.                         ascii(substring('666AAA@@@CB??GG',
  1008.                         2*(d.ss_dtype%35+1)+2-8/c.length,1))
  1009.                         -60),
  1010.             type_name = rtrim(substring(d.type_name,
  1011.                         1+
  1012.                         /*                " I   I I FFMMDD" */
  1013.                         /*                " 1   2 4 484848" */
  1014.                         ascii(substring('III<<<MMMI<<A<A',
  1015.                         2*(d.ss_dtype%35+1)+2-8/c.length,
  1016.                         1))-60, 13)),
  1017.             "precision" = d.data_precision +convert(int,
  1018.                         /*                " I   I I FFMMDD" */
  1019.                         /*                " 1   2 4 484848" */
  1020.                         ascii(substring('???AAAFFFCKFOLS',
  1021.                         2*(d.ss_dtype%35+1)+2-8/c.length,1))-60),
  1022.             length = d.length +convert(int,
  1023.                         /*                " I   I I FFMMDD" */
  1024.                         /*                " 1   2 4 484848" */
  1025.                         ascii(substring('AAA<BB<DDDHLUPP',
  1026.                         2*(d.ss_dtype%35+1)+2-8/c.length,
  1027.                         1))-64),
  1028.             scale = d.numeric_scale,
  1029.             radix = d.numeric_radix,
  1030.             nullable = convert(smallint, 1),
  1031.             remarks = convert(varchar(254),null),    /* Remarks are NULL */
  1032.             ss_data_type = c.type,
  1033.             colid = c.colid
  1034.         FROM
  1035.             syscolumns c,
  1036.             sysobjects o,
  1037.             master.dbo.spt_datatype_info d
  1038.         WHERE
  1039.             o.name like @table_name
  1040.             AND user_name(o.uid) like @table_owner
  1041.             AND o.id = c.id
  1042.             AND c.type = d.ss_dtype
  1043.             AND o.type != 'P'
  1044.             AND c.name like @column_name
  1045.             AND d.ss_dtype IN (111, 109, 38, 110)    /* Just *N types */
  1046.             AND c.usertype < 100            /* No User defined types */
  1047.         UNION ALL
  1048.         SELECT    /* All other types including user data types */
  1049.             table_qualifier = DB_NAME(),
  1050.             table_owner = USER_NAME(o.uid),
  1051.             table_name = o.name,
  1052.             column_name = c.name,
  1053.             data_type = convert(smallint,
  1054.                 /*    Map systypes.type to ODBC type */
  1055.                 /*        SS-Type         "     1 1             1           "*/
  1056.                 /*                        "33 3030    4 44 5 5 255 556666"*/
  1057.                 /*                        "45 7698    5 78 0 2 256 890123"*/
  1058.                         ascii(substring('8;<9?H><<<<:<=6<5<A<??@<GC?GB>',
  1059.                         t.type%34+1,1))-60),
  1060.             type_name = t.name,
  1061.             "precision" = isnull(d.data_precision, convert(int,c.length))
  1062.                         +isnull(d.aux, convert(int,
  1063.                         /*                " I   I I FFMMDD" */
  1064.                         /*                " 1   2 4 484848" */
  1065.                         ascii(substring('???AAAFFFCKFOLS',
  1066.                         2*(d.ss_dtype%35+1)+2-8/c.length,1))-60)),
  1067.             length = isnull(d.length, convert(int,c.length)) +convert(int,
  1068.                         isnull(d.aux,
  1069.                         /*                " I   I I FFMMDD" */
  1070.                         /*                " 1   2 4 484848" */
  1071.                         ascii(substring('AAA<BB<DDDHLUPP',
  1072.                         2*(d.ss_dtype%35+1)+2-8/c.length,
  1073.                         1))-64)),
  1074.             scale = d.numeric_scale +convert(smallint,
  1075.                         isnull(d.aux,
  1076.                         /*                " I   I I FFMMDD" */
  1077.                         /*                " 1   2 4 484848" */
  1078.                         ascii(substring('<<<<<<<<<<<<<<?',
  1079.                         2*(d.ss_dtype%35+1)+2-8/c.length,
  1080.                         1))-60)),
  1081.             radix = d.numeric_radix,
  1082.             nullable =    /* set nullability from status flag */
  1083.                 convert(smallint, convert(bit, c.status&8)),
  1084.             remarks = convert(varchar(254),null),    /* Remarks are NULL */
  1085.             ss_data_type = c.type,
  1086.             colid = c.colid
  1087.         FROM
  1088.             syscolumns c,
  1089.             sysobjects o,
  1090.             master.dbo.spt_datatype_info d,
  1091.             systypes t
  1092.         WHERE
  1093.             o.name like @table_name
  1094.             AND user_name(o.uid) like @table_owner
  1095.             AND o.id = c.id
  1096.             AND c.type = d.ss_dtype
  1097.             AND o.type != 'P'
  1098.             AND c.usertype *= t.usertype
  1099.             AND c.name like @column_name
  1100.             AND (d.ss_dtype NOT IN (111, 109, 38, 110)    /* No *N types */
  1101.                 OR c.usertype >= 100)                    /* User defined types */
  1102.         ORDER BY table_owner, table_name, colid
  1103.     end
  1104. go
  1105.  
  1106. if    (charindex('6.00', @@version) = 0)
  1107. begin
  1108.     print ''
  1109.     print ''
  1110.     print 'Warning:'
  1111.     print 'you are installing the stored procedures '
  1112.     print 'on a pre 6.0 SQL Server.'
  1113.     print 'Ignore the following error.'
  1114. end
  1115. else
  1116.     drop proc sp_columns
  1117. go
  1118.  
  1119. /*    Procedure for 6.0 server */
  1120. CREATE PROCEDURE sp_columns (
  1121.                  @table_name        varchar(90),
  1122.                  @table_owner        varchar(90) = null,
  1123.                  @table_qualifier    varchar(90) = null,
  1124.                  @column_name        varchar(90) = null )
  1125. AS
  1126.     DECLARE @full_table_name    char(181)
  1127.     DECLARE @table_id int
  1128.  
  1129.     if @column_name is null /*    If column name not supplied, match all */
  1130.         select @column_name = '%'
  1131.     if @table_qualifier is not null
  1132.     begin
  1133.         if db_name() != @table_qualifier
  1134.         begin    /* If qualifier doesn't match current database */
  1135.             raiserror (15250, -1,-1,'Table')
  1136.             return
  1137.         end
  1138.     end
  1139.     if @table_name is null
  1140.     begin    /*    If table name not supplied, match all */
  1141.         select @table_name = '%'
  1142.     end
  1143.     if @table_owner is null
  1144.     begin    /* If unqualified table name */
  1145.         SELECT @full_table_name = @table_name
  1146.     end
  1147.     else
  1148.     begin    /* Qualified table name */
  1149.         SELECT @full_table_name = @table_owner + '.' + @table_name
  1150.     end
  1151.  
  1152.     /*    Get Object ID */
  1153.     SELECT @table_id = object_id(@full_table_name)
  1154.     if ((charindex('%',@full_table_name) = 0) and
  1155.         (charindex('_',@full_table_name) = 0)  and
  1156.         @table_id != 0)
  1157.     begin
  1158.         /* this block is for the case where there is no pattern
  1159.             matching required for the table name */
  1160.         SELECT    /* INTn, FLOATn, DATETIMEn and MONEYn types */
  1161.             table_qualifier = DB_NAME(),
  1162.             table_owner = USER_NAME(o.uid),
  1163.             table_name = o.name,
  1164.             column_name = c.name,
  1165.             data_type = d.data_type+convert(smallint,
  1166.                         /*                " I   I I FFMMDD" */
  1167.                         /*                " 1   2 4 484848" */
  1168.                         ascii(substring('666AAA@@@CB??GG',
  1169.                         2*(d.ss_dtype%35+1)+2-8/c.length,1))
  1170.                         -60),
  1171.             type_name = rtrim(substring(d.type_name,
  1172.                         1+
  1173.                         /*                " I   I I FFMMDD" */
  1174.                         /*                " 1   2 4 484848" */
  1175.                         ascii(substring('III<<<MMMI<<A<A',
  1176.                         2*(d.ss_dtype%35+1)+2-8/c.length,
  1177.                         1))-60, 13)),
  1178.             "precision" = convert(int,c.prec),
  1179.             length = d.length + convert(int,
  1180.                         /*                " I   I I FFMMDD" */
  1181.                         /*                " 1   2 4 484848' */
  1182.                         ascii(substring('AAA<BB<DDDHLUPP',
  1183.                         2*(d.ss_dtype%35+1)+2-8/c.length,
  1184.                         1))-64),
  1185.             scale = convert(smallint, c.scale),
  1186.             radix = d.numeric_radix,
  1187.             nullable = convert(smallint, 1),
  1188.             remarks = convert(varchar(254),null),    /* Remarks are NULL */
  1189.             ss_data_type = c.type,
  1190.             colid = c.colid
  1191.         FROM
  1192.             syscolumns c,
  1193.             sysobjects o,
  1194.             master.dbo.spt_datatype_info d
  1195.         WHERE
  1196.             o.id = @table_id
  1197.             AND c.id = o.id
  1198.             AND c.type = d.ss_dtype
  1199.             AND o.type != 'P'
  1200.             AND c.name like @column_name
  1201.             AND d.ss_dtype IN (111, 109, 38, 110)    /* Just *N types */
  1202.             AND c.usertype < 100                    /* No user defined types */
  1203.         UNION ALL
  1204.         SELECT    /* Identity base and decimal/numeric types */
  1205.             table_qualifier = DB_NAME(),
  1206.             table_owner = USER_NAME(o.uid),
  1207.             table_name = o.name,
  1208.             column_name = c.name,
  1209.             data_type = convert(smallint,
  1210.                 /*    Map systypes.type to ODBC type */
  1211.                 /*        SS-Type         "     1 1             1           "*/
  1212.                 /*                        "33 3030    4 44 5 5 255 556666"*/
  1213.                 /*                        "45 7698    5 78 0 2 256 890123"*/
  1214.                         ascii(substring('8;<9?H><<<<:<=6<5<A<??@<GC?GB>',
  1215.                         t.type%34+1,1))-60),
  1216.             type_name = d.type_name,
  1217.             "precision" = convert(int,c.prec),
  1218.             length = convert(int,c.length) +
  1219.                 (convert(int,(c.type & 2)/2) |
  1220.                 (convert(int,c.type&64)/64))*(c.prec+2 - c.length),
  1221.             scale = convert(smallint, c.scale),
  1222.             radix = d.numeric_radix,
  1223.             nullable =    /* set nullability from status flag */
  1224.                 convert(smallint, convert(bit, c.status&8)),
  1225.             remarks = convert(varchar(254),null),    /* Remarks are NULL */
  1226.             ss_data_type = c.type,
  1227.             colid = c.colid
  1228.         FROM
  1229.             syscolumns c,
  1230.             sysobjects o,
  1231.             master.dbo.spt_datatype_info d,
  1232.             systypes t
  1233.         WHERE
  1234.             o.id = @table_id
  1235.             AND c.id = o.id
  1236.             AND c.type = d.ss_dtype
  1237.             AND o.type != 'P'
  1238.             AND d.auto_increment = (c.status&128)/128
  1239.             AND c.usertype = t.usertype
  1240.             AND c.name like @column_name
  1241.             AND ((c.status&128 = 128            /* Identity types only */
  1242.                 AND c.usertype < 100)            /* No user defined types */
  1243.                 OR d.ss_dtype IN (106, 108, 55, 63))     /* decimal/numeric types */
  1244.         UNION ALL
  1245.         SELECT    /* All other types including user data types */
  1246.             table_qualifier = DB_NAME(),
  1247.             table_owner = USER_NAME(o.uid),
  1248.             table_name = o.name,
  1249.             column_name = c.name,
  1250.             data_type = convert(smallint,
  1251.                 /*    Map systypes.type to ODBC type */
  1252.                 /*        SS-Type         "     1 1             1           "*/
  1253.                 /*                        "33 3030    4 44 5 5 255 556666"*/
  1254.                 /*                        "45 7698    5 78 0 2 256 890123"*/
  1255.                         ascii(substring('8;<9?H><<<<:<=6<5<A<??@<GC?GB>',
  1256.                         t.type%34+1,1))-60),
  1257.             type_name = t.name,
  1258.             "precision" = isnull(convert(int,c.prec), 2147483647),
  1259.             length = isnull(d.length, convert(int,c.length)) +convert(int,
  1260.                         isnull(d.aux,
  1261.                         /*                " I   I I FFMMDD" */
  1262.                         /*                " 1   2 4 484848" */
  1263.                         ascii(substring('AAA<BB<DDDHLUPP',
  1264.                         2*(d.ss_dtype%35+1)+2-8/c.length,
  1265.                         1))-64)),
  1266.             scale = convert(smallint, c.scale),
  1267.             radix = d.numeric_radix,
  1268.             nullable =    /* set nullability from status flag */
  1269.                 convert(smallint, convert(bit, c.status&8)),
  1270.             remarks = convert(varchar(254),null),    /* Remarks are NULL */
  1271.             ss_data_type = c.type,
  1272.             colid = c.colid
  1273.         FROM
  1274.             syscolumns c,
  1275.             sysobjects o,
  1276.             master.dbo.spt_datatype_info d,
  1277.             systypes t
  1278.         WHERE
  1279.             o.id = @table_id
  1280.             AND c.id = o.id
  1281.             AND c.type = d.ss_dtype
  1282.             AND o.type != 'P'
  1283.             AND isnull(d.auto_increment,0) = 0
  1284.             AND (c.status&128 = 0            /* No Identity types unless */
  1285.                 OR c.usertype >= 100)        /* User defined type */
  1286.             AND c.usertype *= t.usertype
  1287.             AND c.name like @column_name
  1288.             AND (d.ss_dtype NOT IN (111, 109, 38, 110)    /* No *N types */
  1289.                 OR c.usertype >= 100)                    /* User defined types */
  1290.             AND d.ss_dtype NOT IN (106, 108, 55, 63)    /* No decimal/numeric types */
  1291.         ORDER BY colid
  1292.     end
  1293.     else
  1294.     begin
  1295.         /* this block is for the case where there IS pattern
  1296.             matching done on the table name */
  1297.         if @table_owner is null /*    If owner not supplied, match all */
  1298.             select @table_owner = '%'
  1299.         SELECT    /* INTn, FLOATn, DATETIMEn and MONEYn types */
  1300.             table_qualifier = DB_NAME(),
  1301.             table_owner = USER_NAME(o.uid),
  1302.             table_name = o.name,
  1303.             column_name = c.name,
  1304.             data_type = d.data_type+convert(smallint,
  1305.                         /*                " I   I I FFMMDD" */
  1306.                         /*                " 1   2 4 484848" */
  1307.                         ascii(substring('666AAA@@@CB??GG',
  1308.                         2*(d.ss_dtype%35+1)+2-8/c.length,1))
  1309.                         -60),
  1310.             type_name = rtrim(substring(d.type_name,
  1311.                         1+
  1312.                         /*                " I   I I FFMMDD" */
  1313.                         /*                " 1   2 4 484848" */
  1314.                         ascii(substring('III<<<MMMI<<A<A',
  1315.                         2*(d.ss_dtype%35+1)+2-8/c.length,
  1316.                         1))-60, 13)),
  1317.             "precision" = convert(int,c.prec),
  1318.             length = d.length + convert(int,
  1319.                         /*                " I   I I FFMMDD" */
  1320.                         /*                " 1   2 4 484848" */
  1321.                         ascii(substring('AAA<BB<DDDHLUPP',
  1322.                         2*(d.ss_dtype%35+1)+2-8/c.length,
  1323.                         1))-64),
  1324.             scale = convert(smallint, c.scale),
  1325.             radix = d.numeric_radix,
  1326.             nullable = convert(smallint, 1),
  1327.             remarks = convert(varchar(254),null),    /* Remarks are NULL */
  1328.             ss_data_type = c.type,
  1329.             colid = c.colid
  1330.         FROM
  1331.             syscolumns c,
  1332.             sysobjects o,
  1333.             master.dbo.spt_datatype_info d
  1334.         WHERE
  1335.             o.name like @table_name AND user_name(o.uid) like @table_owner
  1336.             AND o.id = c.id
  1337.             AND c.type = d.ss_dtype
  1338.             AND o.type != 'P'
  1339.             AND c.name like @column_name
  1340.             AND d.ss_dtype IN (111, 109, 38, 110)    /* Just *N types */
  1341.             AND c.usertype < 100            /* No User defined types */
  1342.         UNION ALL
  1343.         SELECT    /* Identity base and decimal/numeric types */
  1344.             table_qualifier = DB_NAME(),
  1345.             table_owner = USER_NAME(o.uid),
  1346.             table_name = o.name,
  1347.             column_name = c.name,
  1348.             data_type = convert(smallint,
  1349.                 /*    Map systypes.type to ODBC type */
  1350.                 /*        SS-Type         "     1 1             1           "*/
  1351.                 /*                        "33 3030    4 44 5 5 255 556666"*/
  1352.                 /*                        "45 7698    5 78 0 2 256 890123"*/
  1353.                         ascii(substring('8;<9?H><<<<:<=6<5<A<??@<GC?GB>',
  1354.                         t.type%34+1,1))-60),
  1355.             type_name = d.type_name,
  1356.             "precision" = convert(int,c.prec),
  1357.             length = convert(int,c.length) +
  1358.                 (convert(int,(c.type & 2)/2) |
  1359.                 (convert(int,c.type&64)/64))*(c.prec+2 - c.length),
  1360.             scale = convert(smallint, c.scale),
  1361.             radix = d.numeric_radix,
  1362.             nullable =    /* set nullability from status flag */
  1363.                 convert(smallint, convert(bit, c.status&8)),
  1364.             remarks = convert(varchar(254),null),    /* Remarks are NULL */
  1365.             ss_data_type = c.type,
  1366.             colid = c.colid
  1367.         FROM
  1368.             syscolumns c,
  1369.             sysobjects o,
  1370.             master.dbo.spt_datatype_info d,
  1371.             systypes t
  1372.         WHERE
  1373.             o.name like @table_name AND user_name(o.uid) like @table_owner
  1374.             AND c.id = o.id
  1375.             AND c.type = d.ss_dtype
  1376.             AND o.type != 'P'
  1377.             AND d.auto_increment = (c.status&128)/128
  1378.             AND c.usertype = t.usertype
  1379.             AND c.name like @column_name
  1380.             AND ((c.status&128 = 128            /* Identity types only */
  1381.                 AND c.usertype < 100)            /* No user defined types */
  1382.                 OR d.ss_dtype IN (106, 108, 55, 63))     /* decimal/numeric types */
  1383.         UNION ALL
  1384.         SELECT    /* All other types including user data types */
  1385.             table_qualifier = DB_NAME(),
  1386.             table_owner = USER_NAME(o.uid),
  1387.             table_name = o.name,
  1388.             column_name = c.name,
  1389.             data_type = convert(smallint,
  1390.                 /*    Map systypes.type to ODBC type */
  1391.                 /*        SS-Type         "     1 1             1           "*/
  1392.                 /*                        "33 3030    4 44 5 5 255 556666"*/
  1393.                 /*                        "45 7698    5 78 0 2 256 890123"*/
  1394.                         ascii(substring('8;<9?H><<<<:<=6<5<A<??@<GC?GB>',
  1395.                         t.type%34+1,1))-60),
  1396.             type_name = t.name,
  1397.             "precision" = isnull(convert(int,c.prec), 2147483647),
  1398.             length = isnull(d.length, convert(int,c.length)) +convert(int,
  1399.                         isnull(d.aux,
  1400.                         /*                " I   I I FFMMDD" */
  1401.                         /*                " 1   2 4 484848" */
  1402.                         ascii(substring('AAA<BB<DDDHLUPP',
  1403.                         2*(d.ss_dtype%35+1)+2-8/c.length,
  1404.                         1))-64)),
  1405.             scale = convert(smallint, c.scale),
  1406.             radix = d.numeric_radix,
  1407.             nullable =    /* set nullability from status flag */
  1408.                 convert(smallint, convert(bit, c.status&8)),
  1409.             remarks = convert(varchar(254),null),    /* Remarks are NULL */
  1410.             ss_data_type = c.type,
  1411.             colid = c.colid
  1412.         FROM
  1413.             syscolumns c,
  1414.             sysobjects o,
  1415.             master.dbo.spt_datatype_info d,
  1416.             systypes t
  1417.         WHERE
  1418.             o.name like @table_name AND user_name(o.uid) like @table_owner
  1419.             AND o.id = c.id
  1420.             AND c.type = d.ss_dtype
  1421.             AND o.type != 'P'
  1422.             AND isnull(d.auto_increment,0) = 0
  1423.             AND (c.status&128 = 0            /* No Identity types unless */
  1424.                 OR c.usertype >= 100)        /* User defined type */
  1425.             AND c.usertype *= t.usertype
  1426.             AND c.name like @column_name
  1427.             AND (d.ss_dtype NOT IN (111, 109, 38, 110)    /* No *N types */
  1428.                 OR c.usertype >= 100)                    /* User defined types */
  1429.             AND d.ss_dtype NOT IN (106, 108, 55, 63)    /* No decimal/numeric types */
  1430.         ORDER BY table_owner, table_name, colid
  1431.     end
  1432. go
  1433.  
  1434. grant execute on sp_columns to public
  1435. go
  1436.  
  1437. dump tran master with no_log
  1438. go
  1439. print 'creating sp_fkeys'
  1440. go
  1441.  
  1442. /*    Procedure for pre-6.0 server */
  1443. CREATE PROCEDURE sp_fkeys(
  1444.                @pktable_name        varchar(32) = null,
  1445.                @pktable_owner        varchar(32) = null,
  1446.                @pktable_qualifier    varchar(32) = null,
  1447.                @fktable_name        varchar(32) = null,
  1448.                @fktable_owner        varchar(32) = null,
  1449.                @fktable_qualifier    varchar(32) = null )
  1450. as
  1451.     declare    @order_by_pk int
  1452.  
  1453.     select  @order_by_pk = 0
  1454.  
  1455.     if (@pktable_name is null) and (@fktable_name is null)
  1456.     begin    /* If neither primary key nor foreign key table names given */
  1457.         raiserror 20004 'PK table name or FK table name must be given.'
  1458.         return
  1459.     end
  1460.     if @fktable_qualifier is not null
  1461.     begin
  1462.         if db_name() != @fktable_qualifier
  1463.         begin    /* If qualifier doesn't match current database */
  1464.             raiserror 20001 'Foreign Key Table qualifier must be name of current database'
  1465.             return
  1466.         end
  1467.     end
  1468.     if @pktable_qualifier is not null
  1469.     begin
  1470.         if db_name() != @pktable_qualifier
  1471.         begin    /* If qualifier doesn't match current database */
  1472.             raiserror 20001 'Primary Key Table qualifier must be name of current database'
  1473.             return
  1474.         end
  1475.     end
  1476.  
  1477.     if @pktable_name is null
  1478.     begin /*  If table name not supplied, match all */
  1479.         select @pktable_name = '%'
  1480.         select @order_by_pk = 1
  1481.     end
  1482.     if @pktable_owner is null    /*    If PK owner not supplied, match all */
  1483.         select @pktable_owner = '%'
  1484.     if @fktable_name is null    /*    If table name not supplied, match all */
  1485.         select @fktable_name = '%'
  1486.     if @fktable_owner is null    /*    If FK owner not supplied, match all */
  1487.         select @fktable_owner = '%'
  1488.  
  1489.     if @@trancount != 0
  1490.     begin    /* If inside a transaction */
  1491.         raiserror 20003 'The procedure ''sp_fkeys'' cannot be executed from within a transaction.'
  1492.         return
  1493.     end
  1494.     create table #fkeys(
  1495.              pktable_qualifier    varchar(32) NULL,
  1496.              pktable_owner        varchar(32) NULL,
  1497.              pktable_name        varchar(32) NOT NULL,
  1498.              pkcolumn_name        varchar(32) NOT NULL,
  1499.              fktable_qualifier    varchar(32) NULL,
  1500.              fktable_owner        varchar(32) NULL,
  1501.              fktable_name        varchar(32) NOT NULL,
  1502.              fkcolumn_name        varchar(32) NOT NULL,
  1503.              key_seq            smallint NOT NULL)
  1504.  
  1505.     /*    SQL Server supports upto 8 PK/FK relationships between 2 tables */
  1506.     /*    Process syskeys for each relationship */
  1507.     /*    The inserts below adds a row to the temp table for each of the
  1508.         8 possible relationships */
  1509.     insert into #fkeys
  1510.         select
  1511.             db_name(),
  1512.             user_name(o1.uid),
  1513.             object_name(k.depid),
  1514.             c2.name,
  1515.             db_name(),
  1516.             user_name(o2.uid),
  1517.             object_name(k.id),
  1518.             c1.name,
  1519.             1
  1520.         from
  1521.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  1522.         where
  1523.             c1.id = k.id
  1524.             and k.type = 2    /* Foreign type key */
  1525.             and c1.colid = k.key1
  1526.             and c2.id = k.depid
  1527.             and c2.colid = k.depkey1
  1528.             and o1.id = k.depid
  1529.             and o2.id = k.id
  1530.     union
  1531.         select
  1532.             db_name(),
  1533.             user_name(o1.uid),
  1534.             object_name(k.depid),
  1535.             c2.name,
  1536.             db_name(),
  1537.             user_name(o2.uid),
  1538.             object_name(k.id),
  1539.             c1.name,
  1540.             2
  1541.         from
  1542.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  1543.         where
  1544.             c1.id = k.id
  1545.             and k.type = 2    /* Foreign type key */
  1546.             and c1.colid = k.key2
  1547.             and c2.id = k.depid
  1548.             and c2.colid = k.depkey2
  1549.             and o1.id = k.depid
  1550.             and o2.id = k.id
  1551.     union
  1552.         select
  1553.             db_name(),
  1554.             user_name(o1.uid),
  1555.             object_name(k.depid),
  1556.             c2.name,
  1557.             db_name(),
  1558.             user_name(o2.uid),
  1559.             object_name(k.id),
  1560.             c1.name,
  1561.             3
  1562.         from
  1563.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  1564.         where
  1565.             c1.id = k.id
  1566.             and k.type = 2    /* Foreign type key */
  1567.             and c1.colid = k.key3
  1568.             and c2.id = k.depid
  1569.             and c2.colid = k.depkey3
  1570.             and o1.id = k.depid
  1571.             and o2.id = k.id
  1572.     union
  1573.         select
  1574.             db_name(),
  1575.             user_name(o1.uid),
  1576.             object_name(k.depid),
  1577.             c2.name,
  1578.             db_name(),
  1579.             user_name(o2.uid),
  1580.             object_name(k.id),
  1581.             c1.name,
  1582.             4
  1583.         from
  1584.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  1585.         where
  1586.             c1.id = k.id
  1587.             and k.type = 2    /* Foreign type key */
  1588.             and c1.colid = k.key4
  1589.             and c2.id = k.depid
  1590.             and c2.colid = k.depkey4
  1591.             and o1.id = k.depid
  1592.             and o2.id = k.id
  1593.     union
  1594.         select
  1595.             db_name(),
  1596.             user_name(o1.uid),
  1597.             object_name(k.depid),
  1598.             c2.name,
  1599.             db_name(),
  1600.             user_name(o2.uid),
  1601.             object_name(k.id),
  1602.             c1.name,
  1603.             5
  1604.         from
  1605.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  1606.         where
  1607.             c1.id = k.id
  1608.             and k.type = 2    /* Foreign type key */
  1609.             and c1.colid = k.key5
  1610.             and c2.id = k.depid
  1611.             and c2.colid = k.depkey5
  1612.             and o1.id = k.depid
  1613.             and o2.id = k.id
  1614.     union
  1615.         select
  1616.             db_name(),
  1617.             user_name(o1.uid),
  1618.             object_name(k.depid),
  1619.             c2.name,
  1620.             db_name(),
  1621.             user_name(o2.uid),
  1622.             object_name(k.id),
  1623.             c1.name,
  1624.             6
  1625.         from
  1626.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  1627.         where
  1628.             c1.id = k.id
  1629.             and k.type = 2    /* Foreign type key */
  1630.             and c1.colid = k.key6
  1631.             and c2.id = k.depid
  1632.             and c2.colid = k.depkey6
  1633.             and o1.id = k.depid
  1634.             and o2.id = k.id
  1635.     union
  1636.         select
  1637.             db_name(),
  1638.             user_name(o1.uid),
  1639.             object_name(k.depid),
  1640.             c2.name,
  1641.             db_name(),
  1642.             user_name(o2.uid),
  1643.             object_name(k.id),
  1644.             c1.name,
  1645.             7
  1646.         from
  1647.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  1648.         where
  1649.             c1.id = k.id
  1650.             and k.type = 2    /* Foreign type key */
  1651.             and c1.colid = k.key7
  1652.             and c2.id = k.depid
  1653.             and c2.colid = k.depkey7
  1654.             and o1.id = k.depid
  1655.             and o2.id = k.id
  1656.     union
  1657.         select
  1658.             db_name(),
  1659.             user_name(o1.uid),
  1660.             object_name(k.depid),
  1661.             c2.name,
  1662.             db_name(),
  1663.             user_name(o2.uid),
  1664.             object_name(k.id),
  1665.             c1.name,
  1666.             8
  1667.         from
  1668.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  1669.         where
  1670.             c1.id = k.id
  1671.             and k.type = 2    /* Foreign type key */
  1672.             and c1.colid = k.key8
  1673.             and c2.id = k.depid
  1674.             and c2.colid = k.depkey8
  1675.             and o1.id = k.depid
  1676.             and o2.id = k.id
  1677.  
  1678.     if @order_by_pk = 1 /*    If order by PK fields */
  1679.         select
  1680.             pktable_qualifier,
  1681.             pktable_owner,
  1682.             pktable_name,
  1683.             pkcolumn_name,
  1684.             fktable_qualifier,
  1685.             fktable_owner,
  1686.             fktable_name,
  1687.             fkcolumn_name,
  1688.             key_seq,
  1689.             update_rule = convert(smallint, null),
  1690.             delete_rule = convert(smallint,null),
  1691.             fk_name = convert(varchar(32),null),
  1692.             pk_name = convert(varchar(32),null)
  1693.         from #fkeys
  1694.         where fktable_name like @fktable_name
  1695.             and fktable_owner like @fktable_owner
  1696.             and pktable_name  like @pktable_name
  1697.             and pktable_owner like @pktable_owner
  1698.         order by pktable_qualifier, pktable_owner, pktable_name, key_seq
  1699.     else        /*    Order by FK fields */
  1700.         select
  1701.             pktable_qualifier,
  1702.             pktable_owner,
  1703.             pktable_name,
  1704.             pkcolumn_name,
  1705.             fktable_qualifier,
  1706.             fktable_owner,
  1707.             fktable_name,
  1708.             fkcolumn_name,
  1709.             key_seq,
  1710.             update_rule = convert(smallint,null),
  1711.             delete_rule = convert(smallint,null),
  1712.             fk_name = convert(varchar(32),null),
  1713.             pk_name = convert(varchar(32),null)
  1714.         from #fkeys
  1715.         where fktable_name like @fktable_name
  1716.             and fktable_owner like @fktable_owner
  1717.             and pktable_name  like @pktable_name
  1718.             and pktable_owner like @pktable_owner
  1719.         order by fktable_qualifier, fktable_owner, fktable_name, key_seq
  1720. go
  1721.  
  1722. if    (charindex('6.00', @@version) = 0)
  1723. begin
  1724.     print ''
  1725.     print ''
  1726.     print 'Warning:'
  1727.     print 'you are installing the stored procedures '
  1728.     print 'on a pre 6.0 SQL Server.'
  1729.     print 'Ignore the following errors.'
  1730. end
  1731. else
  1732.     drop proc sp_fkeys
  1733. go
  1734.  
  1735. /*    Procedure for 6.0 server */
  1736. CREATE PROCEDURE sp_fkeys(
  1737.                @pktable_name        varchar(32) = null,
  1738.                @pktable_owner        varchar(32) = null,
  1739.                @pktable_qualifier    varchar(32) = null,
  1740.                @fktable_name        varchar(32) = null,
  1741.                @fktable_owner        varchar(32) = null,
  1742.                @fktable_qualifier    varchar(32) = null )
  1743. as
  1744.     DECLARE @pktable_id            int
  1745.     DECLARE @pkfull_table_name    char(70)
  1746.     DECLARE @fktable_id            int
  1747.     DECLARE @fkfull_table_name    char(70)
  1748.     declare    @order_by_pk        int
  1749.  
  1750.     select  @order_by_pk = 0
  1751.  
  1752.     if (@pktable_name is null) and (@fktable_name is null)
  1753.     begin    /* If neither primary key nor foreign key table names given */
  1754.         raiserror (15252,-1,-1)
  1755.         return
  1756.     end
  1757.  
  1758.     if @pktable_owner is null
  1759.     begin    /* If unqualified primary key table name */
  1760.         SELECT @pkfull_table_name = @pktable_name
  1761.     end
  1762.     else
  1763.     begin    /* Qualified primary key table name */
  1764.         SELECT @pkfull_table_name = @pktable_owner + '.' + @pktable_name
  1765.     end
  1766.     /*    Get Object ID */
  1767.     SELECT @pktable_id = object_id(@pkfull_table_name)
  1768.  
  1769.     if @fktable_owner is null
  1770.     begin    /* If unqualified foreign key table name */
  1771.         SELECT @fkfull_table_name = @fktable_name
  1772.     end
  1773.     else
  1774.     begin    /* Qualified foreign key table name */
  1775.         SELECT @fkfull_table_name = @fktable_owner + '.' + @fktable_name
  1776.     end
  1777.     /*    Get Object ID */
  1778.     SELECT @fktable_id = object_id(@fkfull_table_name)
  1779.  
  1780.     if @fktable_name is not null
  1781.     begin
  1782.         if @fktable_id is null
  1783.             SELECT @fktable_id = 0    /* fk table not found, empty result */
  1784.     end
  1785.  
  1786.     if @pktable_name is null
  1787.     begin /*  If table name not supplied, match all */
  1788.         select @order_by_pk = 1
  1789.     end
  1790.     else
  1791.     begin
  1792.         if @pktable_id is null
  1793.         begin
  1794.             SELECT @pktable_id = 0    /* pk table not found, empty result */
  1795.         end
  1796.     end
  1797.  
  1798.     if @@trancount != 0
  1799.     begin    /* If inside a transaction */
  1800.         raiserror(15002,-1,-1,'sp_fkeys')
  1801.         return
  1802.     end
  1803.  
  1804.     create table #fkeys(
  1805.              pkdb_id        int NOT NULL,
  1806.              pktable_id     int NOT NULL,
  1807.              pkcolid        int NOT NULL,
  1808.              fkdb_id        int NOT NULL,
  1809.              fktable_id        int NOT NULL,
  1810.              fkcolid        int NOT NULL,
  1811.              key_seq        smallint NOT NULL,
  1812.              fk_name        varchar(32) NULL,
  1813.              pk_name        varchar(32) NULL)
  1814.  
  1815.     /*    SQL Server supports upto 16 PK/FK relationships between 2 tables */
  1816.     /*    Process syskeys for each relationship */
  1817.     /*    The inserts below adds a row to the temp table for each of the
  1818.         16 possible relationships */
  1819.     insert into #fkeys
  1820.         select
  1821.             r.rkeydbid,
  1822.             r.rkeyid,
  1823.             r.rkey1,
  1824.             r.fkeydbid,
  1825.             r.fkeyid,
  1826.             r.fkey1,
  1827.             1,
  1828.             OBJECT_NAME(r.constid),
  1829.             i.name
  1830.         from
  1831.             sysreferences r, sysindexes i
  1832.         where    r.rkeyid = i.id
  1833.             AND (i.status & 0x800) = 0x800
  1834.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  1835.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  1836.       union
  1837.         select
  1838.             r.rkeydbid,
  1839.             r.rkeyid,
  1840.             r.rkey2,
  1841.             r.fkeydbid,
  1842.             r.fkeyid,
  1843.             r.fkey2,
  1844.             2,
  1845.             OBJECT_NAME(r.constid),
  1846.             i.name
  1847.         from
  1848.             sysreferences r, sysindexes i
  1849.         where    r.rkeyid = i.id
  1850.             AND (i.status & 0x800) = 0x800
  1851.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  1852.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  1853.       union
  1854.         select
  1855.             r.rkeydbid,
  1856.             r.rkeyid,
  1857.             r.rkey3,
  1858.             r.fkeydbid,
  1859.             r.fkeyid,
  1860.             r.fkey3,
  1861.             3,
  1862.             OBJECT_NAME(r.constid),
  1863.             i.name
  1864.         from
  1865.             sysreferences r, sysindexes i
  1866.         where    r.rkeyid = i.id
  1867.             AND (i.status & 0x800) = 0x800
  1868.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  1869.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  1870.       union
  1871.         select
  1872.             r.rkeydbid,
  1873.             r.rkeyid,
  1874.             r.rkey4,
  1875.             r.fkeydbid,
  1876.             r.fkeyid,
  1877.             r.fkey4,
  1878.             4,
  1879.             OBJECT_NAME(r.constid),
  1880.             i.name
  1881.         from
  1882.             sysreferences r, sysindexes i
  1883.         where    r.rkeyid = i.id
  1884.             AND (i.status & 0x800) = 0x800
  1885.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  1886.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  1887.       union
  1888.         select
  1889.             r.rkeydbid,
  1890.             r.rkeyid,
  1891.             r.rkey5,
  1892.             r.fkeydbid,
  1893.             r.fkeyid,
  1894.             r.fkey5,
  1895.             5,
  1896.             OBJECT_NAME(r.constid),
  1897.             i.name
  1898.         from
  1899.             sysreferences r, sysindexes i
  1900.         where    r.rkeyid = i.id
  1901.             AND (i.status & 0x800) = 0x800
  1902.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  1903.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  1904.       union
  1905.         select
  1906.             r.rkeydbid,
  1907.             r.rkeyid,
  1908.             r.rkey6,
  1909.             r.fkeydbid,
  1910.             r.fkeyid,
  1911.             r.fkey6,
  1912.             6,
  1913.             OBJECT_NAME(r.constid),
  1914.             i.name
  1915.         from
  1916.             sysreferences r, sysindexes i
  1917.         where    r.rkeyid = i.id
  1918.             AND (i.status & 0x800) = 0x800
  1919.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  1920.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  1921.       union
  1922.         select
  1923.             r.rkeydbid,
  1924.             r.rkeyid,
  1925.             r.rkey7,
  1926.             r.fkeydbid,
  1927.             r.fkeyid,
  1928.             r.fkey7,
  1929.             7,
  1930.             OBJECT_NAME(r.constid),
  1931.             i.name
  1932.         from
  1933.             sysreferences r, sysindexes i
  1934.         where    r.rkeyid = i.id
  1935.             AND (i.status & 0x800) = 0x800
  1936.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  1937.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  1938.       union
  1939.         select
  1940.             r.rkeydbid,
  1941.             r.rkeyid,
  1942.             r.rkey8,
  1943.             r.fkeydbid,
  1944.             r.fkeyid,
  1945.             r.fkey8,
  1946.             8,
  1947.             OBJECT_NAME(r.constid),
  1948.             i.name
  1949.         from
  1950.             sysreferences r, sysindexes i
  1951.         where    r.rkeyid = i.id
  1952.             AND (i.status & 0x800) = 0x800
  1953.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  1954.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  1955.       union
  1956.         select
  1957.             r.rkeydbid,
  1958.             r.rkeyid,
  1959.             r.rkey9,
  1960.             r.fkeydbid,
  1961.             r.fkeyid,
  1962.             r.fkey9,
  1963.             9,
  1964.             OBJECT_NAME(r.constid),
  1965.             i.name
  1966.         from
  1967.             sysreferences r, sysindexes i
  1968.         where    r.rkeyid = i.id
  1969.             AND (i.status & 0x800) = 0x800
  1970.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  1971.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  1972.       union
  1973.         select
  1974.             r.rkeydbid,
  1975.             r.rkeyid,
  1976.             r.rkey10,
  1977.             r.fkeydbid,
  1978.             r.fkeyid,
  1979.             r.fkey10,
  1980.             10,
  1981.             OBJECT_NAME(r.constid),
  1982.             i.name
  1983.         from
  1984.             sysreferences r, sysindexes i
  1985.         where    r.rkeyid = i.id
  1986.             AND (i.status & 0x800) = 0x800
  1987.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  1988.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  1989.       union
  1990.         select
  1991.             r.rkeydbid,
  1992.             r.rkeyid,
  1993.             r.rkey11,
  1994.             r.fkeydbid,
  1995.             r.fkeyid,
  1996.             r.fkey11,
  1997.             11,
  1998.             OBJECT_NAME(r.constid),
  1999.             i.name
  2000.         from
  2001.             sysreferences r, sysindexes i
  2002.         where    r.rkeyid = i.id
  2003.             AND (i.status & 0x800) = 0x800
  2004.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2005.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2006.       union
  2007.         select
  2008.             r.rkeydbid,
  2009.             r.rkeyid,
  2010.             r.rkey12,
  2011.             r.fkeydbid,
  2012.             r.fkeyid,
  2013.             r.fkey12,
  2014.             12,
  2015.             OBJECT_NAME(r.constid),
  2016.             i.name
  2017.         from
  2018.             sysreferences r, sysindexes i
  2019.         where    r.rkeyid = i.id
  2020.             AND (i.status & 0x800) = 0x800
  2021.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2022.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2023.       union
  2024.         select
  2025.             r.rkeydbid,
  2026.             r.rkeyid,
  2027.             r.rkey13,
  2028.             r.fkeydbid,
  2029.             r.fkeyid,
  2030.             r.fkey13,
  2031.             13,
  2032.             OBJECT_NAME(r.constid),
  2033.             i.name
  2034.         from
  2035.             sysreferences r, sysindexes i
  2036.         where    r.rkeyid = i.id
  2037.             AND (i.status & 0x800) = 0x800
  2038.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2039.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2040.       union
  2041.         select
  2042.             r.rkeydbid,
  2043.             r.rkeyid,
  2044.             r.rkey14,
  2045.             r.fkeydbid,
  2046.             r.fkeyid,
  2047.             r.fkey14,
  2048.             14,
  2049.             OBJECT_NAME(r.constid),
  2050.             i.name
  2051.         from
  2052.             sysreferences r, sysindexes i
  2053.         where    r.rkeyid = i.id
  2054.             AND (i.status & 0x800) = 0x800
  2055.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2056.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2057.       union
  2058.         select
  2059.             r.rkeydbid,
  2060.             r.rkeyid,
  2061.             r.rkey15,
  2062.             r.fkeydbid,
  2063.             r.fkeyid,
  2064.             r.fkey15,
  2065.             15,
  2066.             OBJECT_NAME(r.constid),
  2067.             i.name
  2068.         from
  2069.             sysreferences r, sysindexes i
  2070.         where    r.rkeyid = i.id
  2071.             AND (i.status & 0x800) = 0x800
  2072.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2073.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2074.       union
  2075.         select
  2076.             r.rkeydbid,
  2077.             r.rkeyid,
  2078.             r.rkey16,
  2079.             r.fkeydbid,
  2080.             r.fkeyid,
  2081.             r.fkey16,
  2082.             16,
  2083.             OBJECT_NAME(r.constid),
  2084.             i.name
  2085.         from
  2086.             sysreferences r, sysindexes i
  2087.         where    r.rkeyid = i.id
  2088.             AND (i.status & 0x800) = 0x800
  2089.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2090.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2091.  
  2092.     if @order_by_pk = 1 /*    If order by PK fields */
  2093.         select
  2094.             pktable_qualifier = DB_NAME(f.pkdb_id),
  2095.             pktable_owner = USER_NAME(o1.uid),
  2096.             pktable_name = o1.name,
  2097.             pkcolumn_name = c1.name,
  2098.             fktable_qualifier = DB_NAME(f.fkdb_id),
  2099.             fktable_owner = USER_NAME(o2.uid),
  2100.             fktable_name = o2.name,
  2101.             fkcolumn_name = c2.name,
  2102.             key_seq,
  2103.             update_rule = convert(smallint,1),
  2104.             delete_rule = convert(smallint,1),
  2105.             fk_name,
  2106.             pk_name
  2107.         from #fkeys f,
  2108.             sysobjects o1, sysobjects o2,
  2109.             syscolumns c1, syscolumns c2
  2110.         where    o1.id = f.pktable_id
  2111.             AND o2.id = f.fktable_id
  2112.             AND c1.id = f.pktable_id
  2113.             AND c2.id = f.fktable_id
  2114.             AND c1.colid = f.pkcolid
  2115.             AND c2.colid = f.fkcolid
  2116.         order by pktable_qualifier, pktable_owner, pktable_name, key_seq
  2117.     else        /*    Order by FK fields */
  2118.         select
  2119.             pktable_qualifier = DB_NAME(f.pkdb_id),
  2120.             pktable_owner = USER_NAME(o1.uid),
  2121.             pktable_name = o1.name,
  2122.             pkcolumn_name = c1.name,
  2123.             fktable_qualifier = DB_NAME(f.fkdb_id),
  2124.             fktable_owner = USER_NAME(o2.uid),
  2125.             fktable_name = o2.name,
  2126.             fkcolumn_name = c2.name,
  2127.             key_seq,
  2128.             update_rule = convert(smallint,1),
  2129.             delete_rule = convert(smallint,1),
  2130.             fk_name,
  2131.             pk_name
  2132.         from #fkeys f,
  2133.             sysobjects o1, sysobjects o2,
  2134.             syscolumns c1, syscolumns c2
  2135.         where    o1.id = f.pktable_id
  2136.             AND o2.id = f.fktable_id
  2137.             AND c1.id = f.pktable_id
  2138.             AND c2.id = f.fktable_id
  2139.             AND c1.colid = f.pkcolid
  2140.             AND c2.colid = f.fkcolid
  2141.         order by fktable_qualifier, fktable_owner, fktable_name, key_seq
  2142. go
  2143.  
  2144. grant execute on sp_fkeys to public
  2145. go
  2146. dump tran master with no_log
  2147. go
  2148.  
  2149. print 'creating sp_pkeys'
  2150. go
  2151.  
  2152. /*    Procedure for pre-6.0 server */
  2153. CREATE PROCEDURE sp_pkeys(
  2154.                @table_name        varchar(32),
  2155.                @table_owner     varchar(32) = null,
  2156.                @table_qualifier varchar(32) = null )
  2157. as
  2158.     if @table_qualifier is not null
  2159.     begin
  2160.         if db_name() != @table_qualifier
  2161.         begin    /* If qualifier doesn't match current database */
  2162.             raiserror 20001 'Table qualifier must be name of current database'
  2163.             return
  2164.         end
  2165.     end
  2166.     if @table_owner is null /*    If owner not supplied, match all */
  2167.         select @table_owner = '%'
  2168.     if @@trancount != 0
  2169.     begin    /* If inside a transaction */
  2170.         raiserror 20003 'The procedure ''sp_pkeys'' cannot be executed from within a transaction.'
  2171.         return
  2172.     end
  2173.  
  2174.     create table #pkeys(
  2175.              table_qualifier varchar(32) NULL,
  2176.              table_owner     varchar(32) NULL,
  2177.              table_name      varchar(32) NOT NULL,
  2178.              column_name     varchar(32) NOT NULL,
  2179.              key_seq         smallint NOT NULL)
  2180.  
  2181.     /*    SQL Server supports upto 8 PK/FK relationships between 2 tables */
  2182.     /*    Process syskeys for each relationship */
  2183.     /*    The inserts below adds a row to the temp table for each of the
  2184.         8 possible relationships */
  2185.     insert into #pkeys
  2186.         select
  2187.             db_name(),
  2188.             (select user_name(uid) from sysobjects o where o.id = k.id),
  2189.             object_name(k.id),
  2190.             c.name,
  2191.             1
  2192.         from
  2193.             syskeys k, syscolumns c
  2194.         where
  2195.             c.id = k.id
  2196.             and k.type = 1    /* Primary type key */
  2197.             and c.colid = k.key1
  2198.     if (@@rowcount = 0)
  2199.         goto done
  2200.  
  2201.     insert into #pkeys
  2202.         select
  2203.             db_name(),
  2204.             (select user_name(uid) from sysobjects o where o.id = k.id),
  2205.             object_name(k.id),
  2206.             c.name,
  2207.             2
  2208.         from
  2209.             syskeys k, syscolumns c
  2210.         where
  2211.             c.id = k.id
  2212.             and k.type = 1    /* Primary type key */
  2213.             and c.colid = key2
  2214.     if (@@rowcount = 0)
  2215.         goto done
  2216.  
  2217.     insert into #pkeys
  2218.         select
  2219.             db_name(),
  2220.             (select user_name(uid) from sysobjects o where o.id = k.id),
  2221.             object_name(k.id),
  2222.             c.name,
  2223.             3
  2224.         from
  2225.             syskeys k, syscolumns c
  2226.         where
  2227.             c.id = k.id
  2228.             and k.type = 1    /* Primary type key */
  2229.             and c.colid = key3
  2230.     if (@@rowcount = 0)
  2231.         goto done
  2232.  
  2233.     insert into #pkeys
  2234.         select
  2235.             db_name(),
  2236.             (select user_name(uid) from sysobjects o where o.id = k.id),
  2237.             object_name(k.id),
  2238.             c.name,
  2239.             4
  2240.         from
  2241.             syskeys k, syscolumns c
  2242.         where
  2243.             c.id = k.id
  2244.             and k.type = 1    /* Primary type key */
  2245.             and c.colid = key4
  2246.     if (@@rowcount = 0)
  2247.         goto done
  2248.  
  2249.     insert into #pkeys
  2250.         select
  2251.             db_name(),
  2252.             (select user_name(uid) from sysobjects o where o.id = k.id),
  2253.             object_name(k.id),
  2254.             c.name,
  2255.             5
  2256.         from
  2257.             syskeys k, syscolumns c
  2258.         where
  2259.             c.id = k.id
  2260.             and k.type = 1    /* Primary type key */
  2261.             and c.colid = key5
  2262.     if (@@rowcount = 0)
  2263.         goto done
  2264.  
  2265.     insert into #pkeys
  2266.         select
  2267.             db_name(),
  2268.             (select user_name(uid) from sysobjects o where o.id = k.id),
  2269.             object_name(k.id),
  2270.             c.name,
  2271.             6
  2272.         from
  2273.             syskeys k, syscolumns c
  2274.         where
  2275.             c.id = k.id
  2276.             and k.type = 1    /* Primary type key */
  2277.             and c.colid = key6
  2278.     if (@@rowcount = 0)
  2279.         goto done
  2280.  
  2281.     insert into #pkeys
  2282.         select
  2283.             db_name(),
  2284.             (select user_name(uid) from sysobjects o where o.id = k.id),
  2285.             object_name(k.id),
  2286.             c.name,
  2287.             7
  2288.         from
  2289.             syskeys k, syscolumns c
  2290.         where
  2291.             c.id = k.id
  2292.             and k.type = 1    /* Primary type key */
  2293.             and c.colid = key7
  2294.     if (@@rowcount = 0)
  2295.         goto done
  2296.  
  2297.     insert into #pkeys
  2298.          select
  2299.              db_name(),
  2300.              (select user_name(uid) from sysobjects o where o.id = k.id),
  2301.              object_name(k.id),
  2302.              c.name,
  2303.              8
  2304.          from
  2305.              syskeys k, syscolumns c
  2306.          where
  2307.              c.id = k.id
  2308.              and k.type = 1 /* Primary type key */
  2309.              and c.colid = key8
  2310.  
  2311.     done:
  2312.     select
  2313.         table_qualifier,
  2314.         table_owner,
  2315.         table_name,
  2316.         column_name,
  2317.         key_seq,
  2318.         pk_name = convert(varchar(32),null)
  2319.     from #pkeys
  2320.     where table_name = @table_name
  2321.         and table_owner like @table_owner
  2322.     order by table_qualifier, table_owner, table_name, key_seq
  2323. go
  2324.  
  2325. if    (charindex('6.00', @@version) = 0)
  2326. begin
  2327.     print ''
  2328.     print ''
  2329.     print 'Warning:'
  2330.     print 'you are installing the stored procedures '
  2331.     print 'on a pre 6.0 SQL Server.'
  2332.     print 'Ignore the following error.'
  2333. end
  2334. else
  2335.     drop proc sp_pkeys
  2336. go
  2337.  
  2338. /*    Procedure for 6.0 server */
  2339. CREATE PROCEDURE sp_pkeys(
  2340.                @table_name        varchar(32),
  2341.                @table_owner     varchar(32) = null,
  2342.                @table_qualifier varchar(32) = null )
  2343. as
  2344.     DECLARE @table_id            int
  2345.     DECLARE @full_table_name    char(70)
  2346.  
  2347.     if @table_qualifier is not null
  2348.     begin
  2349.         if db_name() != @table_qualifier
  2350.         begin    /* If qualifier doesn't match current database */
  2351.             raiserror (15250, -1,-1,'Table')
  2352.             return
  2353.         end
  2354.     end
  2355.     if @table_owner is null
  2356.     begin    /* If unqualified table name */
  2357.         SELECT @full_table_name = @table_name
  2358.     end
  2359.     else
  2360.     begin    /* Qualified table name */
  2361.         SELECT @full_table_name = @table_owner + '.' + @table_name
  2362.     end
  2363.     /*    Get Object ID */
  2364.     SELECT @table_id = object_id(@full_table_name)
  2365.  
  2366.     select
  2367.         table_qualifier = db_name(),
  2368.         table_owner = user_name(o.uid),
  2369.         table_name = o.name,
  2370.         column_name = c.name,
  2371.         key_seq = convert(smallint,c1.colid),
  2372.         pk_name = i.name
  2373.     from
  2374.         sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  2375.     where
  2376.         o.id = @table_id
  2377.         and o.id = c.id
  2378.         and o.id = i.id
  2379.         and (i.status & 0x800) = 0x800
  2380.         and c.name = index_col (@full_table_name, i.indid, c1.colid)
  2381.         and c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  2382.         and c1.id = @table_id
  2383.     order by table_qualifier, table_owner, table_name, key_seq
  2384. go
  2385.  
  2386. grant execute on sp_pkeys to public
  2387. go
  2388.  
  2389. dump tran master with no_log
  2390. go
  2391.  
  2392. print 'creating sp_stored_procedures'
  2393. go
  2394.  
  2395. create procedure sp_stored_procedures(
  2396.                         @sp_name        varchar(96) = null,
  2397.                         @sp_owner        varchar(90) = null,
  2398.                         @sp_qualifier    varchar(32) = null)
  2399. as
  2400.     declare @proc_type smallint
  2401.  
  2402.     if @sp_qualifier is not null
  2403.     begin
  2404.         if db_name() != @sp_qualifier
  2405.         begin
  2406.             if @sp_qualifier = ''
  2407.             begin
  2408.                 /* in this case, we need to return an empty result set */
  2409.                 /* because the user has requested a database with an empty name */
  2410.                 select @sp_name = ''
  2411.                 select @sp_owner = ''
  2412.             end else
  2413.             begin    /* If qualifier doesn't match current database */
  2414.                 raiserror 20001 'Procedure qualifier must be name of current database'
  2415.                 return
  2416.             end
  2417.         end
  2418.     end
  2419.  
  2420.     if @sp_name is null
  2421.     begin  /*  If procedure name not supplied, match all */
  2422.         select @sp_name = '%'
  2423.     end
  2424.     else begin
  2425.         if (@sp_owner is null) and (charindex('%', @sp_name) = 0)
  2426.         begin
  2427.             if exists (select * from sysobjects
  2428.                 where uid = user_id()
  2429.                     and name = @sp_name
  2430.                     and type = 'P') /* Object type of Procedure */
  2431.             begin
  2432.                 select @sp_owner = user_name()
  2433.             end
  2434.         end
  2435.     end
  2436.     if @sp_owner is null    /*    If procedure owner not supplied, match all */
  2437.         select @sp_owner = '%'
  2438.  
  2439.     select @proc_type=2        /* Return 2 for 4.2 and later servers. */
  2440.  
  2441.     select
  2442.         procedure_qualifier = db_name(),
  2443.         procedure_owner = user_name(o.uid),
  2444.         procedure_name = o.name +';'+ ltrim(str(p.number,5)),
  2445.         num_input_params = -1,    /* Constant since value unknown */
  2446.         num_output_params = -1, /* Constant since value unknown */
  2447.         num_result_sets = -1,    /* Constant since value unknown */
  2448.         remarks = convert(varchar(254),null),    /* Remarks are NULL */
  2449.         procedure_type = @proc_type
  2450.     from
  2451.         sysobjects o,sysprocedures p,sysusers u
  2452.     where
  2453.         o.name like @sp_name
  2454.         and p.sequence = 0
  2455.         and user_name(o.uid) like @sp_owner
  2456.         and o.type = 'P'        /* Object type of Procedure */
  2457.         and p.id = o.id
  2458.         and u.uid = user_id()    /* constrain sysusers uid for use in subquery */
  2459.         and (suser_id() = 1     /* User is the System Administrator */
  2460.             or o.uid = user_id()    /* User created the object */
  2461.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  2462.             or ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  2463.              from sysprotects p
  2464.              /* outer join to correlate with all rows in sysobjects */
  2465.              where p.id =* o.id
  2466.                  /*  get rows for public,current user,user's group */
  2467.                  and (p.uid = 0 or p.uid = user_id() or p.uid =* u.gid)
  2468.                  /* check for SELECT,EXECUTE privilege */
  2469.                  and (action in (193,224)))&1    /* more magic...normalize GRANT */
  2470.             ) = 1     /* final magic...compare Grants    */
  2471.         )
  2472.     order by procedure_qualifier, procedure_owner, procedure_name
  2473. go
  2474. grant execute on sp_stored_procedures to public
  2475. go
  2476.  
  2477. dump tran master with no_log
  2478. go
  2479.  
  2480.  
  2481. print 'creating sp_sproc_columns'
  2482. go
  2483.  
  2484. /*    Procedure for pre-6.0 server */
  2485. CREATE PROCEDURE sp_sproc_columns (
  2486.                  @procedure_name        varchar(96) = '%',
  2487.                  @procedure_owner        varchar(90) = null,
  2488.                  @procedure_qualifier    varchar(32) = null,
  2489.                  @column_name            varchar(90) = null )
  2490. AS
  2491.     DECLARE @group_num int
  2492.     DECLARE @semi_position int
  2493.     DECLARE @full_procedure_name    char(187)
  2494.     DECLARE @procedure_id int
  2495.  
  2496.     if @column_name is null /*    If column name not supplied, match all */
  2497.         select @column_name = '%'
  2498.     if @procedure_qualifier is not null
  2499.     begin
  2500.         if db_name() != @procedure_qualifier
  2501.         begin
  2502.             if @procedure_qualifier = ''
  2503.             begin
  2504.                 /* in this case, we need to return an empty result set */
  2505.                 /* because the user has requested a database with an empty name */
  2506.                 select @procedure_name = ''
  2507.                 select @procedure_owner = ''
  2508.             end
  2509.             else
  2510.             begin    /* If qualifier doesn't match current database */
  2511.                 raiserror 20001 'Procedure qualifier must be name of current database'
  2512.                 return
  2513.             end
  2514.         end
  2515.     end
  2516.  
  2517.     if @procedure_name is null
  2518.     begin    /*    If procedure name not supplied, match all */
  2519.         select @procedure_name = '%'
  2520.     end
  2521.  
  2522.     /* first we need to extract the procedure group number, if one exists */
  2523.     select @semi_position = charindex(';',@procedure_name)
  2524.     if (@semi_position > 0)
  2525.     begin    /* If group number separator (;) found */
  2526.         select @group_num = convert(int,substring(@procedure_name, @semi_position + 1, 2))
  2527.         select @procedure_name = substring(@procedure_name, 1, @semi_position -1)
  2528.     end
  2529.     else
  2530.     begin    /* No group separator, so default to group number of 1 */
  2531.         select @group_num = 1
  2532.     end
  2533.  
  2534.     if @procedure_owner is null
  2535.     begin    /* If unqualified procedure name */
  2536.         SELECT @full_procedure_name = @procedure_name
  2537.     end
  2538.     else
  2539.     begin    /* Qualified procedure name */
  2540.         SELECT @full_procedure_name = @procedure_owner + '.' + @procedure_name
  2541.     end
  2542.  
  2543.     /*    Get Object ID */
  2544.     SELECT @procedure_id = object_id(@full_procedure_name)
  2545.     if ((charindex('%',@full_procedure_name) = 0) and
  2546.         (charindex('_',@full_procedure_name) = 0)  and
  2547.         @procedure_id != 0)
  2548.     begin
  2549.         /* this block is for the case where there is no pattern
  2550.             matching required for the procedure name */
  2551.         SELECT    /* INTn, FLOATn, DATETIMEn and MONEYn types */
  2552.             procedure_qualifier = DB_NAME(),
  2553.             procedure_owner = USER_NAME(o.uid),
  2554.             procedure_name = o.name +';'+ ltrim(str(c.number,5)),
  2555.             column_name = c.name,
  2556.             column_type = convert(smallint, 0),
  2557.             data_type = d.data_type+convert(smallint,
  2558.                         /*                " I   I I FFMMDD" */
  2559.                         /*                " 1   2 4 484848" */
  2560.                         ascii(substring('666AAA@@@CB??GG',
  2561.                         2*(d.ss_dtype%35+1)+2-8/c.length,1))
  2562.                         -60),
  2563.             type_name = rtrim(substring(d.type_name,
  2564.                         1+
  2565.                         /*                " I   I I FFMMDD" */
  2566.                         /*                " 1   2 4 484848" */
  2567.                         ascii(substring('III<<<MMMI<<A<A',
  2568.                         2*(d.ss_dtype%35+1)+2-8/c.length,
  2569.                         1))-60, 13)),
  2570.             "precision" = d.data_precision,
  2571.             length = d.length +convert(int,
  2572.                         /*                " I   I I FFMMDD" */
  2573.                         /*                " 1   2 4 484848" */
  2574.                         ascii(substring('AAA<BB<DDDHLUPP',
  2575.                         2*(d.ss_dtype%35+1)+2-8/c.length,
  2576.                         1))-64),
  2577.             scale = d.numeric_scale,
  2578.             radix = d.numeric_radix,
  2579.             nullable = convert(smallint, 1),
  2580.             remarks = convert(varchar(254),null),    /* Remarks are NULL */
  2581.             ss_data_type = c.type,
  2582.             colid = c.colid
  2583.         FROM
  2584.             syscolumns c,
  2585.             sysobjects o,
  2586.             master.dbo.spt_datatype_info d,
  2587.             systypes t,
  2588.             sysprocedures p
  2589.         WHERE
  2590.             o.id = @procedure_id
  2591.             AND c.id = o.id
  2592.             AND c.usertype = t.usertype
  2593.             AND c.type = d.ss_dtype
  2594.             AND c.name like @column_name
  2595.             AND d.ss_dtype IN (111, 109, 38, 110)    /* Just *N types */
  2596.             AND c.number = @group_num
  2597.         UNION
  2598.         SELECT           /* All other types including user data types */
  2599.             procedure_qualifier = DB_NAME(),
  2600.             procedure_owner = USER_NAME(o.uid),
  2601.             procedure_name = o.name +';'+ ltrim(str(c.number,5)),
  2602.             column_name = c.name,
  2603.             column_type = convert(smallint, 0),
  2604.             data_type = convert(smallint,
  2605.                 /*    Map systypes.type to ODBC type */
  2606.                 /*        SS-Type         "     1 1             1           "*/
  2607.                 /*                        "33 3030    4 44 5 5 255 556666"*/
  2608.                 /*                        "45 7698    5 78 0 2 256 890123"*/
  2609.                         ascii(substring('8;<9?H><<<<:<=6<5<A<??@<GC?GB>',
  2610.                         t.type%34+1,1))-60),
  2611.             type_name = t.name,
  2612.             "precision" = isnull(d.data_precision, convert(int,c.length))
  2613.                         +isnull(d.aux, convert(int,
  2614.                         /*                " I   I I FFMMDD" */
  2615.                         /*                " 1   2 4 484848" */
  2616.                         ascii(substring('???AAAFFFCKFOLS',
  2617.                         2*(d.ss_dtype%35+1)+2-8/c.length,1))-60)),
  2618.             length = isnull(d.length, convert(int,c.length)) +convert(int,
  2619.                         isnull(d.aux,
  2620.                         /*                " I   I I FFMMDD" */
  2621.                         /*                " 1   2 4 484848" */
  2622.                         ascii(substring('AAA<BB<DDDHLUPP',
  2623.                         2*(d.ss_dtype%35+1)+2-8/c.length,
  2624.                         1))-64)),
  2625.             scale = d.numeric_scale +convert(smallint,
  2626.                         isnull(d.aux,
  2627.                         /*                " I   I I FFMMDD" */
  2628.                         /*                " 1   2 4 484848" */
  2629.                         ascii(substring('<<<<<<<<<<<<<<?',
  2630.                         2*(d.ss_dtype%35+1)+2-8/c.length,
  2631.                         1))-60)),
  2632.             radix = d.numeric_radix,
  2633.             d.nullable,
  2634.             remarks = convert(varchar(254),null),    /* Remarks are NULL */
  2635.             ss_data_type = c.type,
  2636.             colid = c.colid
  2637.         FROM
  2638.             syscolumns c,
  2639.             sysobjects o,
  2640.             master.dbo.spt_datatype_info d,
  2641.             systypes t
  2642.         WHERE
  2643.             o.id = @procedure_id
  2644.             AND c.id = o.id
  2645.             AND c.type = d.ss_dtype
  2646.             AND c.usertype *= t.usertype
  2647.             AND c.name like @column_name
  2648.             AND d.ss_dtype NOT IN (111, 109, 38, 110)    /* No *N types */
  2649.             AND c.number = @group_num
  2650.         UNION ALL
  2651.         SELECT           /* return value row*/
  2652.             procedure_qualifier = DB_NAME(),
  2653.             procedure_owner = USER_NAME(o.uid),
  2654.             procedure_name = o.name +';'+ isnull(ltrim(str(c.number,5)),'1'),
  2655.             column_name = convert(varchar(32),'RETURN_VALUE'),
  2656.             column_type = convert(smallint, 5),
  2657.             data_type = convert(smallint, 4),
  2658.             type_name = convert(varchar(32),'int'),
  2659.             "precision" = convert(int,10),
  2660.             length = convert(int,4),
  2661.             scale = convert(smallint,0),
  2662.             radix = convert(smallint,10),
  2663.             nullable = convert(smallint,0),
  2664.             remarks = convert(varchar(254),null),    /* Remarks are NULL */
  2665.             ss_data_type = convert(tinyint,56),
  2666.             colid = convert(tinyint,0)
  2667.         FROM
  2668.             syscolumns c,
  2669.             sysobjects o
  2670.         WHERE
  2671.             o.id = @procedure_id
  2672.             AND c.id =* o.id
  2673.             AND c.colid = 1
  2674.             AND 'RETURN_VALUE' like @column_name
  2675.         ORDER BY procedure_qualifier, procedure_owner, procedure_name, colid
  2676.     end
  2677.     else
  2678.     begin
  2679.         /* this block is for the case where there IS pattern
  2680.             matching done on the procedure name */
  2681.         if @procedure_owner is null
  2682.             select @procedure_owner = '%'
  2683.         SELECT    /* INTn, FLOATn, DATETIMEn and MONEYn types */
  2684.             procedure_qualifier = DB_NAME(),
  2685.             procedure_owner = USER_NAME(o.uid),
  2686.             procedure_name = o.name +';'+ ltrim(str(c.number,5)),
  2687.             column_name = c.name,
  2688.             column_type = convert(smallint, 0),
  2689.             data_type = d.data_type+convert(smallint,
  2690.                         /*                " I   I I FFMMDD" */
  2691.                         /*                " 1   2 4 484848" */
  2692.                         ascii(substring('666AAA@@@CB??GG',
  2693.                         2*(d.ss_dtype%35+1)+2-8/c.length,1))
  2694.                         -60),
  2695.             type_name = rtrim(substring(d.type_name,
  2696.                         1+
  2697.                         /*                " I   I I FFMMDD" */
  2698.                         /*                " 1   2 4 484848" */
  2699.                         ascii(substring('III<<<MMMI<<A<A',
  2700.                         2*(d.ss_dtype%35+1)+2-8/c.length,
  2701.                         1))-60, 13)),
  2702.             "precision" = isnull(d.data_precision, convert(int,c.length))
  2703.                         +isnull(d.aux, convert(int,
  2704.                         /*                " I   I I FFMMDD" */
  2705.                         /*                " 1   2 4 484848" */
  2706.                         ascii(substring('???AAAFFFCKFOLS',
  2707.                         2*(d.ss_dtype%35+1)+2-8/c.length,1))-60)),
  2708.             length = d.length +convert(int,
  2709.                         /*                " I   I I FFMMDD" */
  2710.                         /*                " 1   2 4 484848" */
  2711.                         ascii(substring('AAA<BB<DDDHLUPP',
  2712.                         2*(d.ss_dtype%35+1)+2-8/c.length,
  2713.                         1))-64),
  2714.             scale = d.numeric_scale,
  2715.             radix = d.numeric_radix,
  2716.             nullable = convert(smallint, 1),
  2717.             remarks = convert(varchar(254),null),    /* Remarks are NULL */
  2718.             ss_data_type = c.type,
  2719.             colid = c.colid
  2720.         FROM
  2721.             syscolumns c,
  2722.             sysobjects o,
  2723.             master.dbo.spt_datatype_info d,
  2724.             systypes t
  2725.         WHERE
  2726.             o.name like @procedure_name
  2727.             AND user_name(o.uid) like @procedure_owner
  2728.             AND o.id = c.id
  2729.             AND c.usertype = t.usertype
  2730.             AND c.type = d.ss_dtype
  2731.             AND c.name like @column_name
  2732.             AND o.type = 'P'                        /* Just Procedures */
  2733.             AND d.ss_dtype IN (111, 109, 38, 110)    /* Just *N types */
  2734.         UNION
  2735.         SELECT           /* All other types including user data types */
  2736.             procedure_qualifier = DB_NAME(),
  2737.             procedure_owner = USER_NAME(o.uid),
  2738.             procedure_name = o.name +';'+ ltrim(str(c.number,5)),
  2739.             column_name = c.name,
  2740.             column_type = convert(smallint, 0),
  2741.             data_type = convert(smallint,
  2742.                 /*    Map systypes.type to ODBC type */
  2743.                 /*        SS-Type         "     1 1             1           "*/
  2744.                 /*                        "33 3030    4 44 5 5 255 556666"*/
  2745.                 /*                        "45 7698    5 78 0 2 256 890123"*/
  2746.                         ascii(substring('8;<9?H><<<<:<=6<5<A<??@<GC?GB>',
  2747.                         t.type%34+1,1))-60),
  2748.             type_name = t.name,
  2749.             "precision" = isnull(d.data_precision, convert(int,c.length))
  2750.                         +isnull(d.aux, convert(int,
  2751.                         /*                " I   I I FFMMDD" */
  2752.                         /*                " 1   2 4 484848" */
  2753.                         ascii(substring('???AAAFFFCKFOLS',
  2754.                         2*(d.ss_dtype%35+1)+2-8/c.length,1))-60)),
  2755.             length = isnull(d.length, convert(int,c.length)) +convert(int,
  2756.                         isnull(d.aux,
  2757.                         /*                " I   I I FFMMDD" */
  2758.                         /*                " 1   2 4 484848" */
  2759.                         ascii(substring('AAA<BB<DDDHLUPP',
  2760.                         2*(d.ss_dtype%35+1)+2-8/c.length,
  2761.                         1))-64)),
  2762.             scale = d.numeric_scale +convert(smallint,
  2763.                         isnull(d.aux,
  2764.                         /*                " I   I I FFMMDD" */
  2765.                         /*                " 1   2 4 484848" */
  2766.                         ascii(substring('<<<<<<<<<<<<<<?',
  2767.                         2*(d.ss_dtype%35+1)+2-8/c.length,
  2768.                         1))-60)),
  2769.             radix = d.numeric_radix,
  2770.             d.nullable,
  2771.             remarks = convert(varchar(254),null),    /* Remarks are NULL */
  2772.             ss_data_type = c.type,
  2773.             colid = c.colid
  2774.         FROM
  2775.             syscolumns c,
  2776.             sysobjects o,
  2777.             master.dbo.spt_datatype_info d,
  2778.             systypes t
  2779.         WHERE
  2780.             o.name like @procedure_name
  2781.             AND user_name(o.uid) like @procedure_owner
  2782.             AND o.id = c.id
  2783.             AND c.type = d.ss_dtype
  2784.             AND c.usertype *= t.usertype
  2785.             AND o.type = 'P'                            /* Just Procedures */
  2786.             AND c.name like @column_name
  2787.             AND d.ss_dtype NOT IN (111, 109, 38, 110)    /* No *N types */
  2788.         UNION ALL
  2789.         SELECT           /* return value row*/
  2790.             procedure_qualifier = DB_NAME(),
  2791.             procedure_owner = USER_NAME(o.uid),
  2792.             procedure_name = o.name +';'+ isnull(ltrim(str(c.number,5)),'1'),
  2793.             column_name = convert(varchar(32),'RETURN_VALUE'),
  2794.             column_type = convert(smallint, 5),
  2795.             data_type = convert(smallint, 4),
  2796.             type_name = convert(varchar(32),'int'),
  2797.             "precision" = convert(int,10),
  2798.             length = convert(int,4),
  2799.             scale = convert(smallint,0),
  2800.             radix = convert(smallint,10),
  2801.             nullable = convert(smallint,0),
  2802.             remarks = convert(varchar(254),null),    /* Remarks are NULL */
  2803.             ss_data_type = convert(tinyint,56),
  2804.             colid = convert(tinyint,0)
  2805.         FROM
  2806.             syscolumns c,
  2807.             sysobjects o
  2808.         WHERE
  2809.             o.name like @procedure_name
  2810.             AND user_name(o.uid) like @procedure_owner
  2811.             AND c.id =* o.id
  2812.             AND c.colid = 1
  2813.             AND o.type = 'P'                        /* Just Procedures */
  2814.             AND 'RETURN_VALUE' like @column_name
  2815.         ORDER BY procedure_qualifier, procedure_owner, procedure_name, colid
  2816.     end
  2817. go
  2818.  
  2819. if    (charindex('6.00', @@version) = 0)
  2820. begin
  2821.     print ''
  2822.     print ''
  2823.     print 'Warning:'
  2824.     print 'you are installing the stored procedures '
  2825.     print 'on a pre 6.0 SQL Server.'
  2826.     print 'Ignore the following error.'
  2827. end
  2828. else
  2829.     drop proc sp_sproc_columns
  2830. go
  2831.  
  2832. /*    Procedure for 6.0 server */
  2833. CREATE PROCEDURE sp_sproc_columns (
  2834.                  @procedure_name        varchar(96) = '%',
  2835.                  @procedure_owner        varchar(90) = null,
  2836.                  @procedure_qualifier    varchar(32) = null,
  2837.                  @column_name            varchar(90) = null )
  2838. AS
  2839.     DECLARE @group_num int
  2840.     DECLARE @semi_position int
  2841.     DECLARE @full_procedure_name    char(187)
  2842.     DECLARE @procedure_id int
  2843.  
  2844.     if @column_name is null /*    If column name not supplied, match all */
  2845.         select @column_name = '%'
  2846.     if @procedure_qualifier is not null
  2847.     begin
  2848.         if db_name() != @procedure_qualifier
  2849.         begin
  2850.             if @procedure_qualifier = ''
  2851.             begin
  2852.                 /* in this case, we need to return an empty result set */
  2853.                 /* because the user has requested a database with an empty name */
  2854.                 select @procedure_name = ''
  2855.                 select @procedure_owner = ''
  2856.             end
  2857.             else
  2858.             begin    /* If qualifier doesn't match current database */
  2859.                 raiserror (15250, -1,-1,'Procedure')
  2860.                 return
  2861.             end
  2862.         end
  2863.     end
  2864.  
  2865.     if @procedure_name is null
  2866.     begin    /*    If procedure name not supplied, match all */
  2867.         select @procedure_name = '%'
  2868.     end
  2869.  
  2870.     /* first we need to extract the procedure group number, if one exists */
  2871.     select @semi_position = charindex(';',@procedure_name)
  2872.     if (@semi_position > 0)
  2873.     begin    /* If group number separator (;) found */
  2874.         select @group_num = convert(int,substring(@procedure_name, @semi_position + 1, 2))
  2875.         select @procedure_name = substring(@procedure_name, 1, @semi_position -1)
  2876.     end
  2877.     else
  2878.     begin    /* No group separator, so default to group number of 1 */
  2879.         select @group_num = 1
  2880.     end
  2881.  
  2882.     if @procedure_owner is null
  2883.     begin    /* If unqualified procedure name */
  2884.         SELECT @full_procedure_name = @procedure_name
  2885.     end
  2886.     else
  2887.     begin    /* Qualified procedure name */
  2888.         SELECT @full_procedure_name = @procedure_owner + '.' + @procedure_name
  2889.     end
  2890.  
  2891.     /*    Get Object ID */
  2892.     SELECT @procedure_id = object_id(@full_procedure_name)
  2893.     if ((charindex('%',@full_procedure_name) = 0) and
  2894.         (charindex('_',@full_procedure_name) = 0)  and
  2895.         @procedure_id != 0)
  2896.     begin
  2897.         /* this block is for the case where there is no pattern
  2898.             matching required for the procedure name */
  2899.         SELECT    /* INTn, FLOATn, DATETIMEn and MONEYn types */
  2900.             procedure_qualifier = DB_NAME(),
  2901.             procedure_owner = USER_NAME(o.uid),
  2902.             procedure_name = o.name +';'+ ltrim(str(c.number,5)),
  2903.             column_name = c.name,
  2904.             column_type = convert(smallint, 1+((c.status/64)&1)),
  2905.             data_type = d.data_type+convert(smallint,
  2906.                         /*                " I   I I FFMMDD" */
  2907.                         /*                " 1   2 4 484848" */
  2908.                         ascii(substring('666AAA@@@CB??GG',
  2909.                         2*(d.ss_dtype%35+1)+2-8/c.length,1))
  2910.                         -60),
  2911.             type_name = rtrim(substring(d.type_name,
  2912.                         1+
  2913.                         /*                " I   I I FFMMDD" */
  2914.                         /*                " 1   2 4 484848" */
  2915.                         ascii(substring('III<<<MMMI<<A<A',
  2916.                         2*(d.ss_dtype%35+1)+2-8/c.length,
  2917.                         1))-60, 13)),
  2918.             "precision" = convert(int,c.prec),
  2919.             length = d.length + convert(int,
  2920.                         /*                " I   I I FFMMDD" */
  2921.                         /*                " 1   2 4 484848" */
  2922.                         ascii(substring('AAA<BB<DDDHLUPP',
  2923.                         2*(d.ss_dtype%35+1)+2-8/c.length,
  2924.                         1))-64),
  2925.             scale = convert(smallint, c.scale),
  2926.             radix = d.numeric_radix,
  2927.             nullable = convert(smallint, 1),
  2928.             remarks = convert(varchar(254),null),    /* Remarks are NULL */
  2929.             ss_data_type = c.type,
  2930.             colid = c.colid
  2931.         FROM
  2932.             syscolumns c,
  2933.             sysobjects o,
  2934.             master.dbo.spt_datatype_info d,
  2935.             systypes t,
  2936.             sysprocedures p
  2937.         WHERE
  2938.             o.id = @procedure_id
  2939.             AND c.id = o.id
  2940.             AND c.usertype = t.usertype
  2941.             AND c.type = d.ss_dtype
  2942.             AND isnull(d.auto_increment,0) = 0
  2943.             AND c.name like @column_name
  2944.             AND d.ss_dtype IN (111, 109, 38, 110)    /* Just *N types */
  2945.             AND c.number = @group_num
  2946.         UNION ALL
  2947.         SELECT    /* decimal/numeric types */
  2948.             procedure_qualifier = DB_NAME(),
  2949.             procedure_owner = USER_NAME(o.uid),
  2950.             procedure_name = o.name +';'+ ltrim(str(c.number,5)),
  2951.             column_name = c.name,
  2952.             column_type = convert(smallint, 1+((c.status/64)&1)),
  2953.             data_type = convert(smallint,
  2954.                 /*    Map systypes.type to ODBC type */
  2955.                 /*        SS-Type         "     1 1             1           "*/
  2956.                 /*                        "33 3030    4 44 5 5 255 556666"*/
  2957.                 /*                        "45 7698    5 78 0 2 256 890123"*/
  2958.                         ascii(substring('8;<9?H><<<<:<=6<5<A<??@<GC?GB>',
  2959.                         t.type%34+1,1))-60),
  2960.             type_name = t.name,
  2961.             "precision" = convert(int,c.prec),
  2962.             length = convert(int,c.prec) + 2,
  2963.             scale = convert(smallint, c.scale),
  2964.             radix = d.numeric_radix,
  2965.             d.nullable,
  2966.             remarks = convert(varchar(254),null),    /* Remarks are NULL */
  2967.             ss_data_type = c.type,
  2968.             colid = c.colid
  2969.         FROM
  2970.             syscolumns c,
  2971.             sysobjects o,
  2972.             master.dbo.spt_datatype_info d,
  2973.             systypes t
  2974.         WHERE
  2975.             o.id = @procedure_id
  2976.             AND c.id = o.id
  2977.             AND c.type = d.ss_dtype
  2978.             AND isnull(d.auto_increment,0) = 0
  2979.             AND c.usertype *= t.usertype
  2980.             AND c.name like @column_name
  2981.             AND d.ss_dtype IN (106, 108, 55, 63)     /* decimal/numeric types */
  2982.             AND c.number = @group_num
  2983.         UNION ALL
  2984.         SELECT           /* All other types including user data types */
  2985.             procedure_qualifier = DB_NAME(),
  2986.             procedure_owner = USER_NAME(o.uid),
  2987.             procedure_name = o.name +';'+ ltrim(str(c.number,5)),
  2988.             column_name = c.name,
  2989.             column_type = convert(smallint, 1+((c.status/64)&1)),
  2990.             data_type = convert(smallint,
  2991.                 /*    Map systypes.type to ODBC type */
  2992.                 /*        SS-Type         "     1 1             1           "*/
  2993.                 /*                        "33 3030    4 44 5 5 255 556666"*/
  2994.                 /*                        "45 7698    5 78 0 2 256 890123"*/
  2995.                         ascii(substring('8;<9?H><<<<:<=6<5<A<??@<GC?GB>',
  2996.                         t.type%34+1,1))-60),
  2997.             type_name = t.name,
  2998.             "precision" = isnull(convert(int,c.prec), 2147483647),
  2999.             length = isnull(d.length, convert(int,c.length)) +convert(int,
  3000.                         isnull(d.aux,
  3001.                         /*                " I   I I FFMMDD" */
  3002.                         /*                " 1   2 4 484848" */
  3003.                         ascii(substring('AAA<BB<DDDHLUPP',
  3004.                         2*(d.ss_dtype%35+1)+2-8/c.length,
  3005.                         1))-64)),
  3006.             scale = convert(smallint, c.scale),
  3007.             radix = d.numeric_radix,
  3008.             d.nullable,
  3009.             remarks = convert(varchar(254),null),    /* Remarks are NULL */
  3010.             ss_data_type = c.type,
  3011.             colid = c.colid
  3012.         FROM
  3013.             syscolumns c,
  3014.             sysobjects o,
  3015.             master.dbo.spt_datatype_info d,
  3016.             systypes t
  3017.         WHERE
  3018.             o.id = @procedure_id
  3019.             AND c.id = o.id
  3020.             AND c.type = d.ss_dtype
  3021.             AND isnull(d.auto_increment,0) = 0
  3022.             AND c.usertype *= t.usertype
  3023.             AND c.name like @column_name
  3024.             AND d.ss_dtype NOT IN (111, 109, 38, 110, 106, 108, 55, 63)    /* No *N types */
  3025.             AND c.number = @group_num
  3026.         UNION ALL
  3027.         SELECT           /* return value row*/
  3028.             procedure_qualifier = DB_NAME(),
  3029.             procedure_owner = USER_NAME(o.uid),
  3030.             procedure_name = o.name +';'+ isnull(ltrim(str(c.number,5)),'1'),
  3031.             column_name = convert(varchar(32),'RETURN_VALUE'),
  3032.             column_type = convert(smallint, 5),
  3033.             data_type = convert(smallint, 4),
  3034.             type_name = convert(varchar(32),'int'),
  3035.             "precision" = convert(int,10),
  3036.             length = convert(int,4),
  3037.             scale = convert(smallint,0),
  3038.             radix = convert(smallint,10),
  3039.             nullable = convert(smallint,0),
  3040.             remarks = convert(varchar(254),null),    /* Remarks are NULL */
  3041.             ss_data_type = convert(tinyint,56),
  3042.             colid = convert(tinyint,0)
  3043.         FROM
  3044.             syscolumns c,
  3045.             sysobjects o
  3046.         WHERE
  3047.             o.id = @procedure_id
  3048.             AND c.id =* o.id
  3049.             AND c.colid = 1
  3050.             AND 'RETURN_VALUE' like @column_name
  3051.         ORDER BY procedure_qualifier, procedure_owner, procedure_name, colid
  3052.     end
  3053.     else
  3054.     begin
  3055.         /* this block is for the case where there IS pattern
  3056.             matching done on the procedure name */
  3057.         if @procedure_owner is null
  3058.             select @procedure_owner = '%'
  3059.         SELECT    /* INTn, FLOATn, DATETIMEn and MONEYn types */
  3060.             procedure_qualifier = DB_NAME(),
  3061.             procedure_owner = USER_NAME(o.uid),
  3062.             procedure_name = o.name +';'+ ltrim(str(c.number,5)),
  3063.             column_name = c.name,
  3064.             column_type = convert(smallint, 1+((c.status/64)&1)),
  3065.             data_type = d.data_type+convert(smallint,
  3066.                         /*                " I   I I FFMMDD" */
  3067.                         /*                " 1   2 4 484848" */
  3068.                         ascii(substring('666AAA@@@CB??GG',
  3069.                         2*(d.ss_dtype%35+1)+2-8/c.length,1))
  3070.                         -60),
  3071.             type_name = rtrim(substring(d.type_name,
  3072.                         1+
  3073.                         /*                " I   I I FFMMDD" */
  3074.                         /*                " 1   2 4 484848" */
  3075.                         ascii(substring('III<<<MMMI<<A<A',
  3076.                         2*(d.ss_dtype%35+1)+2-8/c.length,
  3077.                         1))-60, 13)),
  3078.             "precision" = convert(int,c.prec),
  3079.             length = d.length + convert(int,
  3080.                         /*                " I   I I FFMMDD" */
  3081.                         /*                " 1   2 4 484848" */
  3082.                         ascii(substring('AAA<BB<DDDHLUPP',
  3083.                         2*(d.ss_dtype%35+1)+2-8/c.length,
  3084.                         1))-64),
  3085.             scale = convert(smallint, c.scale),
  3086.             radix = d.numeric_radix,
  3087.             nullable = convert(smallint, 1),
  3088.             remarks = convert(varchar(254),null),    /* Remarks are NULL */
  3089.             ss_data_type = c.type,
  3090.             colid = c.colid
  3091.         FROM
  3092.             syscolumns c,
  3093.             sysobjects o,
  3094.             master.dbo.spt_datatype_info d,
  3095.             systypes t
  3096.         WHERE
  3097.             o.name like @procedure_name
  3098.             AND user_name(o.uid) like @procedure_owner
  3099.             AND o.id = c.id
  3100.             AND c.usertype = t.usertype
  3101.             AND isnull(d.auto_increment,0) = 0
  3102.             AND c.type = d.ss_dtype
  3103.             AND c.name like @column_name
  3104.             AND o.type = 'P'                        /* Just Procedures */
  3105.             AND d.ss_dtype IN (111, 109, 38, 110)    /* Just *N types */
  3106.         UNION ALL
  3107.         SELECT    /* decimal/numeric types */
  3108.             procedure_qualifier = DB_NAME(),
  3109.             procedure_owner = USER_NAME(o.uid),
  3110.             procedure_name = o.name +';'+ ltrim(str(c.number,5)),
  3111.             column_name = c.name,
  3112.             column_type = convert(smallint, 1+((c.status/64)&1)),
  3113.             data_type = convert(smallint,
  3114.                 /*    Map systypes.type to ODBC type */
  3115.                 /*        SS-Type         "     1 1             1           "*/
  3116.                 /*                        "33 3030    4 44 5 5 255 556666"*/
  3117.                 /*                        "45 7698    5 78 0 2 256 890123"*/
  3118.                         ascii(substring('8;<9?H><<<<:<=6<5<A<??@<GC?GB>',
  3119.                         t.type%34+1,1))-60),
  3120.             type_name = t.name,
  3121.             "precision" = convert(int,c.prec),
  3122.             length = convert(int,c.prec) + 2,
  3123.             scale = convert(smallint, c.scale),
  3124.             radix = d.numeric_radix,
  3125.             d.nullable,
  3126.             remarks = convert(varchar(254),null),    /* Remarks are NULL */
  3127.             ss_data_type = c.type,
  3128.             colid = c.colid
  3129.         FROM
  3130.             syscolumns c,
  3131.             sysobjects o,
  3132.             master.dbo.spt_datatype_info d,
  3133.             systypes t
  3134.         WHERE
  3135.             o.name like @procedure_name
  3136.             AND user_name(o.uid) like @procedure_owner
  3137.             AND c.id = o.id
  3138.             AND c.type = d.ss_dtype
  3139.             AND isnull(d.auto_increment,0) = 0
  3140.             AND c.usertype *= t.usertype
  3141.             AND o.type = 'P'                            /* Just Procedures */
  3142.             AND c.name like @column_name
  3143.             AND d.ss_dtype IN (106, 108, 55, 63)     /* decimal/numeric types */
  3144.         UNION ALL
  3145.         SELECT           /* All other types including user data types */
  3146.             procedure_qualifier = DB_NAME(),
  3147.             procedure_owner = USER_NAME(o.uid),
  3148.             procedure_name = o.name +';'+ ltrim(str(c.number,5)),
  3149.             column_name = c.name,
  3150.             column_type = convert(smallint, 1+((c.status/64)&1)),
  3151.             data_type = convert(smallint,
  3152.                 /*    Map systypes.type to ODBC type */
  3153.                 /*        SS-Type         "     1 1             1           "*/
  3154.                 /*                        "33 3030    4 44 5 5 255 556666"*/
  3155.                 /*                        "45 7698    5 78 0 2 256 890123"*/
  3156.                         ascii(substring('8;<9?H><<<<:<=6<5<A<??@<GC?GB>',
  3157.                         t.type%34+1,1))-60),
  3158.             type_name = t.name,
  3159.             "precision" = isnull(convert(int,c.prec), 2147483647),
  3160.             length = isnull(d.length, convert(int,c.length)) +convert(int,
  3161.                         isnull(d.aux,
  3162.                         /*                " I   I I FFMMDD" */
  3163.                         /*                " 1   2 4 484848" */
  3164.                         ascii(substring('AAA<BB<DDDHLUPP',
  3165.                         2*(d.ss_dtype%35+1)+2-8/c.length,
  3166.                         1))-64)),
  3167.             scale = convert(smallint, c.scale),
  3168.             radix = d.numeric_radix,
  3169.             d.nullable,
  3170.             remarks = convert(varchar(254),null),    /* Remarks are NULL */
  3171.             ss_data_type = c.type,
  3172.             colid = c.colid
  3173.         FROM
  3174.             syscolumns c,
  3175.             sysobjects o,
  3176.             master.dbo.spt_datatype_info d,
  3177.             systypes t
  3178.         WHERE
  3179.             o.name like @procedure_name
  3180.             AND user_name(o.uid) like @procedure_owner
  3181.             AND o.id = c.id
  3182.             AND c.type = d.ss_dtype
  3183.             AND isnull(d.auto_increment,0) = 0
  3184.             AND c.usertype *= t.usertype
  3185.             AND o.type = 'P'                            /* Just Procedures */
  3186.             AND c.name like @column_name
  3187.             AND d.ss_dtype NOT IN (111, 109, 38, 110, 106, 108, 55, 63)    /* No *N types */
  3188.         UNION ALL
  3189.         SELECT           /* return value row*/
  3190.             procedure_qualifier = DB_NAME(),
  3191.             procedure_owner = USER_NAME(o.uid),
  3192.             procedure_name = o.name +';'+ isnull(ltrim(str(c.number,5)),'1'),
  3193.             column_name = convert(varchar(32),'RETURN_VALUE'),
  3194.             column_type = convert(smallint, 5),
  3195.             data_type = convert(smallint, 4),
  3196.             type_name = convert(varchar(32),'int'),
  3197.             "precision" = convert(int,10),
  3198.             length = convert(int,4),
  3199.             scale = convert(smallint,0),
  3200.             radix = convert(smallint,10),
  3201.             nullable = convert(smallint,0),
  3202.             remarks = convert(varchar(254),null),    /* Remarks are NULL */
  3203.             ss_data_type = convert(tinyint,56),
  3204.             colid = convert(tinyint,0)
  3205.         FROM
  3206.             syscolumns c,
  3207.             sysobjects o
  3208.         WHERE
  3209.             o.name like @procedure_name
  3210.             AND user_name(o.uid) like @procedure_owner
  3211.             AND c.id =* o.id
  3212.             AND c.colid = 1
  3213.             AND o.type = 'P'                        /* Just Procedures */
  3214.             AND 'RETURN_VALUE' like @column_name
  3215.         ORDER BY procedure_qualifier, procedure_owner, procedure_name, colid
  3216.     end
  3217. go
  3218.  
  3219. grant execute on sp_sproc_columns to public
  3220. go
  3221.  
  3222. print 'creating sp_table_privileges'
  3223. go
  3224.  
  3225. CREATE PROCEDURE sp_table_privileges (
  3226.             @table_name         varchar(90),
  3227.             @table_owner        varchar(90) = null,
  3228.             @table_qualifier    varchar(32) = null)
  3229. as
  3230.  
  3231.     declare @table_id    int,
  3232.             @owner_id     int,
  3233.             @full_table_name char(181)
  3234.     declare @refconst int
  3235.  
  3236.     select @refconst = 1
  3237.     if    (charindex('6.00', @@version) = 0)
  3238.         select @refconst = NULL
  3239.  
  3240.     if @table_qualifier is not null
  3241.     begin
  3242.         if db_name() != @table_qualifier
  3243.         begin    /* If qualifier doesn't match current database */
  3244.             raiserror 20001 'Table qualifier must be name of current database'
  3245.             return
  3246.         end
  3247.     end
  3248.     if @table_owner is null
  3249.     begin    /* If unqualified table name */
  3250.         SELECT @full_table_name = @table_name
  3251.     end
  3252.     else
  3253.     begin    /* Qualified table name */
  3254.         SELECT @full_table_name = @table_owner + '.' + @table_name
  3255.     end
  3256.     /*    Get Object ID */
  3257.     SELECT @table_id = object_id(@full_table_name)
  3258.  
  3259.     if @@trancount != 0
  3260.     begin    /* If inside a transaction */
  3261.         raiserror 20003 'The procedure ''sp_table_privileges'' cannot be executed from within a transaction.'
  3262.         return
  3263.     end
  3264.     create table #table_priv1(
  3265.         table_qualifier            varchar(32) NOT NULL,
  3266.         table_owner             varchar(32) NOT NULL,
  3267.         table_name                varchar(32) NOT NULL,
  3268.         grantor                 varchar(32) NOT NULL,
  3269.         grantee                 varchar(32) NOT NULL,
  3270.         select_privilege        int NOT NULL,
  3271.         insert_privilege        int NOT NULL,
  3272.         update_privilege        int NOT NULL,
  3273.         delete_privilege        int NOT NULL,
  3274.         references_privilege    int NULL,
  3275.         is_grantable            varchar(3) NOT NULL,
  3276.         uid                     int NOT NULL,
  3277.         gid                     int NOT NULL)
  3278.  
  3279.     insert into #table_priv1
  3280.         select distinct
  3281.             db_name(),
  3282.             user_name(o.uid),
  3283.             o.name,
  3284.             user_name(o.uid),
  3285.             u.name,
  3286.             0,
  3287.             0,
  3288.             0,
  3289.             0,
  3290.             @refconst,
  3291.             'no',
  3292.             u.uid,
  3293.             u.gid
  3294.         from sysusers u, sysobjects o
  3295.         where o.id = @table_id and u.uid != u.gid
  3296.            and sysstat & 0xf in (1,2,3)    /* only valid for system tables,
  3297.                                            ** user tables, and views. */
  3298.  
  3299.     /*
  3300.     ** now add row for table owner
  3301.     */
  3302.     if exists (
  3303.         select *
  3304.             from #table_priv1
  3305.             where grantor = grantee)
  3306.     begin
  3307.         update #table_priv1
  3308.         set
  3309.             select_privilege = 1,
  3310.             update_privilege = 1,
  3311.             insert_privilege = 1,
  3312.             delete_privilege = 1,
  3313.             references_privilege = 1,
  3314.             is_grantable = 'yes'
  3315.         where grantor = grantee
  3316.     end
  3317.     else
  3318.     begin
  3319.         insert into #table_priv1
  3320.             select    db_name(),
  3321.                 user_name(o.uid),
  3322.                 o.name,
  3323.                 user_name(o.uid),
  3324.                 user_name(o.uid),
  3325.                 1,
  3326.                 1,
  3327.                 1,
  3328.                 1,
  3329.                 @refconst,
  3330.                 'yes',
  3331.                 o.uid,
  3332.                 u.gid
  3333.             from sysobjects o, sysusers u
  3334.             where o.id = @table_id and u.uid = o.uid
  3335.             and sysstat & 0xf in (1,2,3)    /* only valid for system tables,
  3336.                                            ** user tables, and views. */
  3337.  
  3338.     end
  3339.  
  3340.     update #table_priv1
  3341.     set select_privilege = 1
  3342.     where
  3343.         exists (
  3344.             select * from sysprotects
  3345.             where
  3346.                 id = @table_id
  3347.                 and (#table_priv1.uid = uid
  3348.                     or #table_priv1.gid = uid
  3349.                     or uid = 0)
  3350.                 and protecttype = 205
  3351.                 and action = 193)
  3352.         and not exists (
  3353.             select * from sysprotects
  3354.             where
  3355.                 id = @table_id
  3356.                 and (#table_priv1.uid = uid
  3357.                     or #table_priv1.gid = uid
  3358.                     or uid = 0)
  3359.                 and protecttype = 206
  3360.                 and action = 193)
  3361.  
  3362.     update #table_priv1
  3363.     set insert_privilege = 1
  3364.     where
  3365.         exists (
  3366.             select * from sysprotects
  3367.             where
  3368.                 id = @table_id
  3369.                 and (#table_priv1.uid = uid
  3370.                     or #table_priv1.gid = uid
  3371.                     or uid = 0)
  3372.                 and protecttype = 205
  3373.                 and action = 195)
  3374.         and not exists (
  3375.             select * from sysprotects
  3376.             where
  3377.                 id = @table_id
  3378.                 and (#table_priv1.uid = uid
  3379.                     or #table_priv1.gid = uid
  3380.                     or uid = 0)
  3381.                 and protecttype = 206
  3382.                 and action = 195)
  3383.  
  3384.     update #table_priv1
  3385.     set delete_privilege = 1
  3386.     where
  3387.         exists (
  3388.             select * from sysprotects
  3389.             where
  3390.                 id = @table_id
  3391.                 and (#table_priv1.uid = uid
  3392.                     or #table_priv1.gid = uid
  3393.                     or uid = 0)
  3394.                 and protecttype = 205
  3395.                 and action = 196)
  3396.         and not exists (select * from sysprotects
  3397.             where
  3398.                 id = @table_id
  3399.                 and (#table_priv1.uid = uid
  3400.                     or #table_priv1.gid = uid
  3401.                     or uid = 0)
  3402.                 and protecttype = 206
  3403.                 and action = 196)
  3404.  
  3405.     update #table_priv1
  3406.     set update_privilege = 1
  3407.     where
  3408.         exists (
  3409.             select * from sysprotects
  3410.             where
  3411.                 id = @table_id
  3412.                 and (#table_priv1.uid = uid
  3413.                     or #table_priv1.gid = uid
  3414.                     or uid = 0)
  3415.                 and protecttype = 205
  3416.                 and action = 197)
  3417.         and not exists (
  3418.             select * from sysprotects
  3419.             where
  3420.                 id = @table_id
  3421.                 and (#table_priv1.uid = uid
  3422.                     or #table_priv1.gid = uid
  3423.                     or uid = 0)
  3424.                 and protecttype = 206
  3425.                 and action = 197)
  3426.  
  3427.     update #table_priv1
  3428.     set references_privilege = 1
  3429.     where
  3430.         exists (
  3431.             select * from sysprotects
  3432.             where
  3433.                 id = @table_id
  3434.                 and (#table_priv1.uid = uid
  3435.                     or #table_priv1.gid = uid
  3436.                     or uid = 0)
  3437.                 and protecttype = 205
  3438.                 and action = 26)
  3439.         and not exists (
  3440.             select * from sysprotects
  3441.             where
  3442.                 id = @table_id
  3443.                 and (#table_priv1.uid = uid
  3444.                     or #table_priv1.gid = uid
  3445.                     or uid = 0)
  3446.                 and protecttype = 206
  3447.                 and action = 26)
  3448.  
  3449.     create table #table_priv2(
  3450.         table_qualifier varchar(32) NULL,
  3451.         table_owner     varchar(32) NULL,
  3452.         table_name        varchar(32) NOT NULL,
  3453.         grantor         varchar(32) NULL,
  3454.         grantee         varchar(32) NOT NULL,
  3455.         privilege        varchar(32) NOT NULL,
  3456.         is_grantable    varchar(3) NULL)
  3457.  
  3458.     insert into #table_priv2
  3459.         select
  3460.             table_qualifier,
  3461.             table_owner,
  3462.             table_name,
  3463.             grantor,
  3464.             grantee,
  3465.             'SELECT',
  3466.             is_grantable
  3467.         from #table_priv1
  3468.         where select_privilege = 1
  3469.  
  3470.  
  3471.     insert into #table_priv2
  3472.         select
  3473.             table_qualifier,
  3474.             table_owner,
  3475.             table_name,
  3476.             grantor,
  3477.             grantee,
  3478.             'INSERT',
  3479.             is_grantable
  3480.         from #table_priv1
  3481.         where insert_privilege = 1
  3482.  
  3483.  
  3484.     insert into #table_priv2
  3485.         select
  3486.             table_qualifier,
  3487.             table_owner,
  3488.             table_name,
  3489.             grantor,
  3490.             grantee,
  3491.             'DELETE',
  3492.             is_grantable
  3493.         from #table_priv1
  3494.         where delete_privilege = 1
  3495.  
  3496.  
  3497.     insert into #table_priv2
  3498.         select
  3499.             table_qualifier,
  3500.             table_owner,
  3501.             table_name,
  3502.             grantor,
  3503.             grantee,
  3504.             'UPDATE',
  3505.             is_grantable
  3506.         from #table_priv1
  3507.         where update_privilege = 1
  3508.  
  3509.     insert into #table_priv2
  3510.         select
  3511.             table_qualifier,
  3512.             table_owner,
  3513.             table_name,
  3514.             grantor,
  3515.             grantee,
  3516.             'REFERENCES',
  3517.             is_grantable
  3518.         from #table_priv1
  3519.         where references_privilege = 1
  3520.  
  3521.  
  3522.     select * from #table_priv2
  3523.     order by privilege
  3524. go
  3525.  
  3526. grant execute on sp_table_privileges to public
  3527. go
  3528.  
  3529. dump tran master with no_log
  3530. go
  3531.  
  3532. print 'creating sp_column_privileges'
  3533. go
  3534.  
  3535.  
  3536. CREATE PROCEDURE sp_column_privileges (
  3537.             @table_name         varchar(32),
  3538.             @table_owner        varchar(32) = null,
  3539.             @table_qualifier    varchar(32) = null,
  3540.             @column_name        varchar(90) = null)
  3541. as
  3542.  
  3543.     declare @table_id    int,
  3544.     @owner_id    int
  3545.     DECLARE @full_table_name    char(70)
  3546.     declare @low int                    /* range of userids to check */
  3547.     declare @high int
  3548.     declare @objid int                  /* id of @name if object */
  3549.     declare @owner_name char(32)
  3550.     declare @refconst int
  3551.  
  3552.     select @low = 0, @high = 32767
  3553.  
  3554.     select @refconst = 1
  3555.     if    (charindex('6.00', @@version) = 0)
  3556.         select @refconst = NULL
  3557.  
  3558.     if @column_name is null /*    If column name not supplied, match all */
  3559.         select @column_name = '%'
  3560.  
  3561.     if @table_qualifier is not null
  3562.     begin
  3563.         if db_name() != @table_qualifier
  3564.         begin    /* If qualifier doesn't match current database */
  3565.             raiserror 20001 'Table qualifier must be name of current database'
  3566.             return
  3567.         end
  3568.     end
  3569.     if @table_owner is null
  3570.     begin    /* If unqualified table name */
  3571.         SELECT @full_table_name = @table_name
  3572.     end
  3573.     else
  3574.     begin    /* Qualified table name */
  3575.         SELECT @full_table_name = @table_owner + '.' + @table_name
  3576.     end
  3577.     /*    Get Object ID */
  3578.     select @table_id = object_id(@full_table_name)
  3579.  
  3580.     if @@trancount != 0
  3581.     begin    /* If inside a transaction */
  3582.         raiserror 20003 'The procedure ''sp_column_privileges'' cannot be executed from within a transaction.'
  3583.         return
  3584.     end
  3585.  
  3586.     /*
  3587.     ** We need to create a table which will contain a row for every row to
  3588.     ** be returned to the client.
  3589.     */
  3590.  
  3591.     create table #column_priv1(
  3592.         table_qualifier            varchar(32) NOT NULL,
  3593.         table_owner             varchar(32) NOT NULL,
  3594.         table_name                varchar(32) NOT NULL,
  3595.         column_name             varchar(32) NOT NULL,
  3596.         grantor                 varchar(32) NOT NULL,
  3597.         grantee                 varchar(32) NOT NULL,
  3598.         select_privilege        int NOT NULL,
  3599.         select_grantable        int NOT NULL,
  3600.         insert_privilege        int NOT NULL,
  3601.         insert_grantable        int NOT NULL,
  3602.         update_privilege        int NOT NULL,
  3603.         update_grantable        int NOT NULL,
  3604.         references_privilege    int null,
  3605.         references_grantable    int null,
  3606.         uid                     int NOT NULL,
  3607.         gid                     int NOT NULL,
  3608.         is_grantable            varchar(3) NOT NULL)
  3609.  
  3610. /*
  3611. ** insert a row for the table owner (who has all permissions)
  3612. */
  3613.     select @owner_name = (
  3614.         select user_name(uid)
  3615.         from sysobjects
  3616.         where id = @table_id)
  3617.  
  3618.     insert into #column_priv1
  3619.         select
  3620.             db_name(),
  3621.             @owner_name,
  3622.             @table_name,
  3623.             name,
  3624.             @owner_name,
  3625.             @owner_name,
  3626.             1,
  3627.             1,
  3628.             1,
  3629.             1,
  3630.             1,
  3631.             1,
  3632.             @refconst,
  3633.             @refconst,
  3634.             user_id(@owner_name),
  3635.             0,
  3636.             'yes'
  3637.         from syscolumns
  3638.         where id = @table_id
  3639.  
  3640. /*
  3641. ** now stick a row in the table for every user in the database
  3642. ** we will need to weed out those who have no permissions later
  3643. ** (and yes this is a cartesion product: the uid field in sysprotects
  3644. ** can also have a group id, in which case we need to extend those
  3645. ** privileges to all group members).
  3646. */
  3647.  
  3648.     insert into #column_priv1
  3649.         select distinct
  3650.             db_name(),
  3651.             user_name(o.uid),
  3652.             @table_name,
  3653.             c.name,
  3654.             user_name(o.uid),
  3655.             u.name,
  3656.             0,
  3657.             0,
  3658.             0,
  3659.             0,
  3660.             0,
  3661.             0,
  3662.             0,
  3663.             0,
  3664.             u.uid,
  3665.             u.gid,
  3666.             'no'
  3667.         from sysusers u, syscolumns c, sysobjects o
  3668.         where o.id = @table_id
  3669.             and c.id = o.id
  3670.             and u.gid != u.uid
  3671.             and u.name != @owner_name
  3672.  
  3673.     /*
  3674.     ** we need to create another temporary table to contain all the various
  3675.     ** protection information for the table in question
  3676.     */
  3677.     create table #protects (
  3678.                 uid         smallint NOT NULL,
  3679.                 action        tinyint NOT NULL,
  3680.                 protecttype tinyint NOT NULL,
  3681.                 name        varchar(32) NOT NULL)
  3682.  
  3683.     insert into #protects
  3684.         select
  3685.             p.uid,
  3686.             p.action,
  3687.             p.protecttype,
  3688.             isnull(col_name(id, c.number), 'All')
  3689.             from
  3690.                 sysprotects p,
  3691.                 master.dbo.spt_values c,
  3692.                 master.dbo.spt_values a,
  3693.                 master.dbo.spt_values b
  3694.             where
  3695.                 convert(tinyint, substring(isnull(p.columns, 0x1), c.low, 1))
  3696.                     & c.high != 0
  3697.                     and c.number <= (
  3698.                         select count(*)
  3699.                         from syscolumns
  3700.                         where id = @table_id)
  3701.                 and a.type = 'T'
  3702.                 and a.number = p.action
  3703.                 and p.action in (193,195,197,26)
  3704.                 and b.type = 'T'
  3705.                 and b.number = p.protecttype
  3706.                 and p.id = @table_id
  3707.                 and p.uid between @low and @high
  3708.  
  3709.  
  3710.     update #column_priv1
  3711.     set select_privilege = 1
  3712.     where
  3713.         exists (
  3714.             select * from #protects
  3715.             where
  3716.                 protecttype = 205
  3717.                 and action = 193
  3718.                 and (name = #column_priv1.column_name
  3719.                     or name = 'All')
  3720.                 and (uid = 0
  3721.                     or uid = #column_priv1.gid
  3722.                     or uid = #column_priv1.uid))
  3723.         and not exists (
  3724.             select * from #protects
  3725.             where
  3726.                 protecttype = 206
  3727.                 and action = 193
  3728.                 and (name = #column_priv1.column_name
  3729.                     or name = 'All')
  3730.                 and ( uid = 0
  3731.                     or uid = #column_priv1.gid
  3732.                     or uid = #column_priv1.uid))
  3733.  
  3734.     update #column_priv1
  3735.     set insert_privilege = 1
  3736.     where
  3737.         exists (
  3738.             select * from #protects
  3739.             where
  3740.                 protecttype = 205
  3741.                 and action = 195
  3742.                 and (name = #column_priv1.column_name
  3743.                     or name = 'All')
  3744.                 and (uid = 0
  3745.                     or uid = #column_priv1.gid
  3746.                     or uid = #column_priv1.uid))
  3747.         and not exists (
  3748.             select * from #protects
  3749.             where
  3750.                 protecttype = 206
  3751.                 and action = 195
  3752.                 and (name = #column_priv1.column_name
  3753.                     or name = 'All')
  3754.                 and (uid = 0
  3755.                     or uid = #column_priv1.gid
  3756.                     or uid = #column_priv1.uid))
  3757.  
  3758.     update #column_priv1
  3759.     set update_privilege = 1
  3760.         where
  3761.             exists (
  3762.                 select * from #protects
  3763.                 where protecttype = 205
  3764.                 and action = 197
  3765.                 and (name = #column_priv1.column_name
  3766.                     or name = 'All')
  3767.                 and (uid = 0
  3768.                     or uid = #column_priv1.gid
  3769.                     or uid = #column_priv1.uid))
  3770.             and not exists (
  3771.                 select * from #protects
  3772.                     where protecttype = 206
  3773.                     and action = 197
  3774.                     and (name = #column_priv1.column_name
  3775.                         or name = 'All')
  3776.                     and (uid = 0
  3777.                         or uid = #column_priv1.gid
  3778.                         or uid = #column_priv1.uid))
  3779.  
  3780.     update #column_priv1
  3781.     set references_privilege = 1
  3782.         where
  3783.             exists (
  3784.                 select * from #protects
  3785.                 where protecttype = 205
  3786.                 and action = 26
  3787.                 and (name = #column_priv1.column_name
  3788.                     or name = 'All')
  3789.                 and (uid = 0
  3790.                     or uid = #column_priv1.gid
  3791.                     or uid = #column_priv1.uid))
  3792.             and not exists (
  3793.                 select * from #protects
  3794.                     where protecttype = 206
  3795.                     and action = 26
  3796.                     and (name = #column_priv1.column_name
  3797.                         or name = 'All')
  3798.                     and (uid = 0
  3799.                         or uid = #column_priv1.gid
  3800.                         or uid = #column_priv1.uid))
  3801.  
  3802.     create table #column_priv2(
  3803.         table_qualifier varchar(32) NULL,
  3804.         table_owner     varchar(32) NULL,
  3805.         table_name        varchar(32) NOT NULL,
  3806.         column_name     varchar(32) NOT NULL,
  3807.         grantor         varchar(32) NULL,
  3808.         grantee         varchar(32) NOT NULL,
  3809.         privilege        varchar(32) NOT NULL,
  3810.         is_grantable    varchar(3) NULL)
  3811.  
  3812.     insert into #column_priv2
  3813.         select
  3814.             table_qualifier,
  3815.             table_owner,
  3816.             table_name,
  3817.             column_name,
  3818.             grantor,
  3819.             grantee,
  3820.             'SELECT',
  3821.             is_grantable
  3822.         from #column_priv1
  3823.         where select_privilege = 1
  3824.  
  3825.     insert into #column_priv2
  3826.         select
  3827.             table_qualifier,
  3828.             table_owner,
  3829.             table_name,
  3830.             column_name,
  3831.             grantor,
  3832.             grantee,
  3833.             'INSERT',
  3834.             is_grantable
  3835.         from #column_priv1
  3836.         where insert_privilege = 1
  3837.  
  3838.     insert into #column_priv2
  3839.         select
  3840.             table_qualifier,
  3841.             table_owner,
  3842.             table_name,
  3843.             column_name,
  3844.             grantor,
  3845.             grantee,
  3846.             'UPDATE',
  3847.             is_grantable
  3848.         from #column_priv1
  3849.         where update_privilege = 1
  3850.  
  3851.     insert into #column_priv2
  3852.         select
  3853.             table_qualifier,
  3854.             table_owner,
  3855.             table_name,
  3856.             column_name,
  3857.             grantor,
  3858.             grantee,
  3859.             'REFERENCES',
  3860.             is_grantable
  3861.         from #column_priv1
  3862.         where references_privilege = 1
  3863.  
  3864.     select * from #column_priv2
  3865.     where column_name like @column_name
  3866.     order by column_name, privilege
  3867. go
  3868.  
  3869. grant execute on sp_column_privileges to public
  3870. go
  3871.  
  3872. dump tran master with no_log
  3873. go
  3874.  
  3875.  
  3876. print 'creating sp_server_info'
  3877. go
  3878.  
  3879. create proc sp_server_info (
  3880.             @attribute_id  int = null)
  3881. as
  3882.     if @attribute_id is not null
  3883.         select *
  3884.         from master.dbo.spt_server_info
  3885.         where attribute_id = @attribute_id
  3886.     else
  3887.         select *
  3888.         from master.dbo.spt_server_info
  3889.         order by attribute_id
  3890. go
  3891.  
  3892. grant execute on sp_server_info to public
  3893. go
  3894.  
  3895. print 'creating sp_datatype_info'
  3896. go
  3897.  
  3898. /* the messiness of 'data_type' was to get around the problem of
  3899. returning the correct lengths for user defined types.  the join
  3900. on the type name ensures all user defined types are returned, but
  3901. this puts a null in the data_type column.  by forcing an embedded
  3902. select and correlating it with the current row in systypes, we get
  3903. the correct data_type mapping even for user defined types  (kwg) */
  3904.  
  3905. /*    Procedure for pre-6.0 server */
  3906. create proc sp_datatype_info
  3907.     (@data_type int = 0)
  3908. as
  3909.     if @data_type = 0
  3910.                select    /* Real SQL Server data types */
  3911.             type_name = t.name,
  3912.             d.data_type,
  3913.             "precision" = isnull(d.data_precision, convert(int,t.length)),
  3914.             d.literal_prefix,
  3915.             d.literal_suffix,
  3916.             e.create_params,
  3917.             d.nullable,
  3918.             d.case_sensitive,
  3919.             d.searchable,
  3920.             d.unsigned_attribute,
  3921.             d.money,
  3922.             d.auto_increment,
  3923.             d.local_type_name,
  3924.             minimum_scale = d.numeric_scale,
  3925.             maximum_scale = d.numeric_scale,
  3926.             t.usertype
  3927.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  3928.         where
  3929.             d.ss_dtype = t.type
  3930.             and t.usertype *= e.user_type
  3931.             and t.usertype not in (80,18)        /* No SYSNAME or TIMESTAMP */
  3932.             and t.usertype < 100                /* No user defined types */
  3933.             and t.type not in (111,109,38,110)    /* get rid of nullable types */
  3934.         UNION ALL
  3935.         select    /* SQL Server SYSNAME, TIMESTAMP and user data types */
  3936.             type_name = t.name,
  3937.             d.data_type,
  3938.             "precision" = isnull(d.data_precision, convert(int,t.length)),
  3939.             d.literal_prefix,
  3940.             d.literal_suffix,
  3941.             e.create_params,
  3942.             d.nullable,
  3943.             d.case_sensitive,
  3944.             d.searchable,
  3945.             d.unsigned_attribute,
  3946.             d.money,
  3947.             d.auto_increment,
  3948.             t.name,
  3949.             minimum_scale = d.numeric_scale,
  3950.             maximum_scale = d.numeric_scale,
  3951.             t.usertype
  3952.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  3953.         where
  3954.             d.ss_dtype = t.type
  3955.             and t.usertype *= e.user_type
  3956.             and (t.usertype in (80,18)            /* SYSNAME or TIMESTAMP */
  3957.                 or t.usertype >= 100)            /* User defined types */
  3958.             and t.type not in (111,109,38,110)    /* get rid of nullable types */
  3959.         order by d.data_type, d.auto_increment, d.money, t.usertype
  3960.  
  3961.     else
  3962.         select    /* Real SQL Server data types */
  3963.             type_name = t.name,
  3964.             d.data_type,
  3965.             "precision" = isnull(d.data_precision, convert(int,t.length)),
  3966.             d.literal_prefix,
  3967.             d.literal_suffix,
  3968.             e.create_params,
  3969.             d.nullable,
  3970.             d.case_sensitive,
  3971.             d.searchable,
  3972.             d.unsigned_attribute,
  3973.             d.money,
  3974.             d.auto_increment,
  3975.             d.local_type_name,
  3976.             minimum_scale = d.numeric_scale,
  3977.             maximum_scale = d.numeric_scale,
  3978.             t.usertype
  3979.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  3980.         where
  3981.             data_type = @data_type
  3982.             and d.ss_dtype = t.type
  3983.             and t.usertype *= e.user_type
  3984.             and t.usertype not in (80,18)        /* No SYSNAME or TIMESTAMP */
  3985.             and t.usertype < 100                /* No user defined types */
  3986.             and t.type not in (111,109,38,110)    /* get rid of nullable types */
  3987.         UNION ALL
  3988.         select    /* SQL Server SYSNAME, TIMESTAMP and user data types */
  3989.             type_name = t.name,
  3990.             d.data_type,
  3991.             "precision" = isnull(d.data_precision, convert(int,t.length)),
  3992.             d.literal_prefix,
  3993.             d.literal_suffix,
  3994.             e.create_params,
  3995.             d.nullable,
  3996.             d.case_sensitive,
  3997.             d.searchable,
  3998.             d.unsigned_attribute,
  3999.             d.money,
  4000.             d.auto_increment,
  4001.             t.name,
  4002.             minimum_scale = d.numeric_scale,
  4003.             maximum_scale = d.numeric_scale,
  4004.             t.usertype
  4005.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  4006.         where
  4007.             data_type = @data_type
  4008.             and d.ss_dtype = t.type
  4009.             and t.usertype *= e.user_type
  4010.             and (t.usertype in (80,18)            /* SYSNAME or TIMESTAMP */
  4011.                 or t.usertype >= 100)            /* User defined types */
  4012.             and t.type not in (111,109,38,110)    /* get rid of nullable types */
  4013.         order by d.auto_increment, d.money, t.usertype
  4014.  
  4015. go
  4016.  
  4017. if    (charindex('6.00', @@version) = 0)
  4018. begin
  4019.     print ''
  4020.     print ''
  4021.     print 'Warning:'
  4022.     print 'you are installing the stored procedures '
  4023.     print 'on a pre 6.0 SQL Server.'
  4024.     print 'Ignore the following errors.'
  4025. end
  4026. else
  4027.     drop proc sp_datatype_info
  4028. go
  4029.  
  4030. /*    Procedure for 6.0 server */
  4031. create proc sp_datatype_info
  4032.     (@data_type int = 0, @dummy int = 0)
  4033. as
  4034.     if @data_type = 0
  4035.                select    /* Real SQL Server data types */
  4036.             type_name = d.type_name,
  4037.             d.data_type,
  4038.             "precision" = isnull(convert(int,t.prec+d.numdec*(@@max_precision-convert(int,t.prec))), 2147483647),
  4039.             d.literal_prefix,
  4040.             d.literal_suffix,
  4041.             e.create_params,
  4042.             nullable = t.allownulls & (~(isnull(d.auto_increment,0))),
  4043.             d.case_sensitive,
  4044.             d.searchable,
  4045.             d.unsigned_attribute,
  4046.             d.money,
  4047.             d.auto_increment,
  4048.             d.local_type_name,
  4049.             minimum_scale = d.numeric_scale,
  4050.             maximum_scale = convert(smallint,(t.scale+d.numdec*(@@max_precision-convert(int,t.scale)))*(isnull(d.auto_increment,0)^1)),
  4051.             t.usertype
  4052.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  4053.         where
  4054.             d.ss_dtype = t.type
  4055.             and t.usertype *= e.user_type
  4056.             and isnull(d.auto_increment,0) *= e.auto_increment
  4057.             and t.usertype not in (80,18,24,10)        /* No SYSNAME, TIMESTAMP, DECIMAL,    NUMERIC*/
  4058.             and t.usertype < 100                /* No user defined types */
  4059.             and t.type not in (111,109,38,110)    /* get rid of nullable types */
  4060.         UNION ALL
  4061.         select    /* SQL Server SYSNAME, TIMESTAMP and user data types */
  4062.             type_name = t.name,
  4063.             d.data_type,
  4064.             "precision" = isnull(convert(int,t.prec+d.numdec*(@@max_precision-convert(int,t.prec))), 2147483647),
  4065.             d.literal_prefix,
  4066.             d.literal_suffix,
  4067.             e.create_params,
  4068.             nullable = t.allownulls & (~(isnull(d.auto_increment,0))),
  4069.             d.case_sensitive,
  4070.             d.searchable,
  4071.             d.unsigned_attribute,
  4072.             d.money,
  4073.             d.auto_increment,
  4074.             t.name,
  4075.             minimum_scale = d.numeric_scale,
  4076.             maximum_scale = convert(smallint,(t.scale+d.numdec*(@@max_precision-convert(int,t.scale)))*(isnull(d.auto_increment,0)^1)),
  4077.             t.usertype
  4078.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  4079.         where
  4080.             d.ss_dtype = t.type
  4081.             and isnull(d.auto_increment,0) = 0
  4082.             and t.usertype *= e.user_type
  4083.             and (t.usertype in (80,18)            /* SYSNAME or TIMESTAMP */
  4084.                 or t.usertype >= 100)            /* User defined types */
  4085.             and t.type not in (111,109,38,110)    /* get rid of nullable types */
  4086.         order by d.data_type, d.auto_increment, d.money, t.usertype
  4087.  
  4088.     else
  4089.         select    /* Real SQL Server data types */
  4090.             type_name = d.type_name,
  4091.             d.data_type,
  4092.             "precision" = isnull(convert(int,t.prec+d.numdec*(@@max_precision-convert(int,t.prec))), 2147483647),
  4093.             d.literal_prefix,
  4094.             d.literal_suffix,
  4095.             e.create_params,
  4096.             nullable = t.allownulls & (~(isnull(d.auto_increment,0))),
  4097.             d.case_sensitive,
  4098.             d.searchable,
  4099.             d.unsigned_attribute,
  4100.             d.money,
  4101.             d.auto_increment,
  4102.             d.local_type_name,
  4103.             minimum_scale = d.numeric_scale,
  4104.             maximum_scale = convert(smallint,(t.scale+d.numdec*(@@max_precision-convert(int,t.scale)))*(isnull(d.auto_increment,0)^1)),
  4105.             t.usertype
  4106.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  4107.         where
  4108.             data_type = @data_type
  4109.             and d.ss_dtype = t.type
  4110.             and t.usertype *= e.user_type
  4111.             and isnull(d.auto_increment,0) *= e.auto_increment
  4112.             and t.usertype not in (80,18,24,10)        /* No SYSNAME, TIMESTAMP, DECIMAL,    NUMERIC*/
  4113.             and t.usertype < 100                /* No user defined types */
  4114.             and t.type not in (111,109,38,110)    /* get rid of nullable types */
  4115.         UNION ALL
  4116.         select    /* SQL Server SYSNAME, TIMESTAMP and user data types */
  4117.             type_name = t.name,
  4118.             d.data_type,
  4119.             "precision" = isnull(convert(int,t.prec+d.numdec*(@@max_precision-convert(int,t.prec))), 2147483647),
  4120.             d.literal_prefix,
  4121.             d.literal_suffix,
  4122.             e.create_params,
  4123.             nullable = t.allownulls & (~(isnull(d.auto_increment,0))),
  4124.             d.case_sensitive,
  4125.             d.searchable,
  4126.             d.unsigned_attribute,
  4127.             d.money,
  4128.             d.auto_increment,
  4129.             t.name,
  4130.             minimum_scale = d.numeric_scale,
  4131.             maximum_scale = convert(smallint,(t.scale+d.numdec*(@@max_precision-convert(int,t.scale)))*(isnull(d.auto_increment,0)^1)),
  4132.             t.usertype
  4133.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  4134.         where
  4135.             data_type = @data_type
  4136.             and d.ss_dtype = t.type
  4137.             and isnull(d.auto_increment,0) = 0
  4138.             and t.usertype *= e.user_type
  4139.             and (t.usertype in (80,18)            /* SYSNAME or TIMESTAMP */
  4140.                 or t.usertype >= 100)            /* User defined types */
  4141.             and t.type not in (111,109,38,110)    /* get rid of nullable types */
  4142.         order by d.auto_increment, d.money, t.usertype
  4143.  
  4144. go
  4145.  
  4146.  
  4147. grant execute on sp_datatype_info to public
  4148. go
  4149.  
  4150. dump tran master with no_log
  4151. go
  4152.  
  4153. print 'creating sp_special_columns'
  4154. go
  4155.  
  4156. dump tran master with no_log
  4157. go
  4158.  
  4159. /*    Procedure for pre-6.0 server */
  4160. CREATE PROCEDURE sp_special_columns (
  4161.                  @table_name        varchar(32),
  4162.                  @table_owner        varchar(32) = null,
  4163.                  @table_qualifier    varchar(32) = null,
  4164.                  @col_type            char(1) = 'R',
  4165.                  @scope                char(1) = 'T',
  4166.                  @nullable            char(1) = 'U')
  4167. AS
  4168.     DECLARE @indid                int
  4169.     DECLARE @table_id            int
  4170.     DECLARE @full_table_name    char(70)
  4171.     DECLARE @msg                char(70)
  4172.     DECLARE @scopeout            smallint
  4173.  
  4174.     if @col_type not in ('R','V')
  4175.         begin
  4176.             raiserror 20002 'Illegal ''col_type'' specified -- must be ''R'' or ''V''.'
  4177.             return
  4178.         end
  4179.  
  4180.     if @scope = 'C'
  4181.         select @scopeout = 0
  4182.     else if @scope = 'T'
  4183.         select @scopeout = 1
  4184.     else
  4185.         begin
  4186.             raiserror 20002 'Illegal ''scope'' specified -- must be ''C'' or ''T''.'
  4187.             return
  4188.         end
  4189.  
  4190.     if @nullable not in ('U','O')
  4191.         begin
  4192.             raiserror 20002 'Illegal ''nullable'' specified -- must be ''U'' or ''O''.'
  4193.             return
  4194.         end
  4195.  
  4196.     if @table_qualifier is not null
  4197.        begin
  4198.           if db_name() != @table_qualifier
  4199.               begin    /* If qualifier doesn't match current database */
  4200.                 raiserror 20001 'Table qualifier must be name of current database'
  4201.                 return
  4202.               end
  4203.        end
  4204.     if @table_owner is null
  4205.        begin    /* If unqualified table name */
  4206.           SELECT @full_table_name = @table_name
  4207.        end
  4208.     else
  4209.        begin    /* Qualified table name */
  4210.           SELECT @full_table_name = @table_owner + '.' + @table_name
  4211.        end
  4212.     /*    Get Object ID */
  4213.     SELECT @table_id = object_id(@full_table_name)
  4214.  
  4215.     if @col_type = 'V'
  4216.     BEGIN /* if ROWVER, just run that query */
  4217.         SELECT
  4218.             scope = convert(smallint,NULL),
  4219.             column_name = c.name,
  4220.             data_type = convert(smallint, -3),
  4221.             type_name = t.name,
  4222.             "precision" = convert(int,8),
  4223.             length = convert(int,8),
  4224.             scale = convert(smallint, NULL),
  4225.             pseudo_column = convert(smallint,1)
  4226.         FROM
  4227.             systypes t, syscolumns c, master.dbo.spt_datatype_info d
  4228.         WHERE
  4229.             c.id = @table_id
  4230.             AND c.type = d.ss_dtype
  4231.             AND c.usertype = 80 /*    TIMESTAMP */
  4232.             AND t.usertype = 80 /*    TIMESTAMP */
  4233.         RETURN
  4234.     END
  4235.  
  4236.     /* ROWID, now find the id of the 'best' index for this table */
  4237.  
  4238.     IF @nullable = 'O'    /* Don't include any indexes that contain
  4239.                            nullable columns. */
  4240.  
  4241.             SELECT @indid = MIN(indid)
  4242.                 FROM sysindexes i,syscolumns c,syscolumns c2
  4243.                 WHERE
  4244.                     i.status&2 = 2        /*    If Unique Index */
  4245.                      AND c.id = i.id
  4246.                      AND c2.id = c.id
  4247.                      AND c2.colid < i.keycnt + (i.status&16)/16
  4248.                     AND i.id = @table_id
  4249.                     AND indid > 0        /*    Eliminate Table Row */
  4250.                     AND c.name = index_col(@table_name,i.indid,c2.colid)
  4251.                     GROUP BY indid HAVING SUM(c.status&8) = 0
  4252.  
  4253.     ELSE    /* Include indexes that are partially nullable. */
  4254.  
  4255.         SELECT @indid = MIN(indid)
  4256.             FROM sysindexes i
  4257.             WHERE
  4258.                 status&2 = 2        /*    If Unique Index */
  4259.                 AND id = @table_id
  4260.                 AND indid > 0        /*    Eliminate Table Row */
  4261.  
  4262.     SELECT
  4263.         scope = @scopeout,
  4264.         column_name = INDEX_COL(@full_table_name,indid,c.colid),
  4265.         data_type = d.data_type+convert(smallint,
  4266.                         isnull(d.aux,
  4267.                         /*                " I   I I FFMMDD" */
  4268.                         /*                " 1   2 4 484848" */
  4269.                         ascii(substring('666AAA@@@CB??GG',
  4270.                         2*(d.ss_dtype%35+1)+2-8/c2.length,1))
  4271.                         -60)),
  4272.         type_name = rtrim(substring(d.type_name,
  4273.                         1+isnull(d.aux,
  4274.                         /*                " I   I I FFMMDD" */
  4275.                         /*                " 1   2 4 484848" */
  4276.                         ascii(substring('III<<<MMMI<<A<A',
  4277.                         2*(d.ss_dtype%35+1)+2-8/c2.length,
  4278.                         1))-60), 13)),
  4279.         "precision" = isnull(d.data_precision, convert(int,c2.length))
  4280.                         +isnull(d.aux, convert(int,
  4281.                         /*                " I   I I FFMMDD" */
  4282.                         /*                " 1   2 4 484848" */
  4283.                         ascii(substring('???AAAFFFCKFOLS',
  4284.                         2*(d.ss_dtype%35+1)+2-8/c2.length,1))-60)),
  4285.         length = isnull(d.length, convert(int,c2.length)) +convert(int,
  4286.                         isnull(d.aux,
  4287.                         /*                " I   I I FFMMDD" */
  4288.                         /*                " 1   2 4 484848" */
  4289.                         ascii(substring('AAA<BB<DDDHLUPP',
  4290.                         2*(d.ss_dtype%35+1)+2-8/c2.length,
  4291.                         1))-64)),
  4292.         scale = d.numeric_scale +convert(smallint,
  4293.                         isnull(d.aux,
  4294.                         /*                " I   I I FFMMDD" */
  4295.                         /*                " 1   2 4 484848" */
  4296.                         ascii(substring('<<<<<<<<<<<<<<?',
  4297.                         2*(d.ss_dtype%35+1)+2-8/c2.length,
  4298.                         1))-60)),
  4299.         pseudo_column = convert(smallint,1)
  4300.     FROM
  4301.         sysindexes x,
  4302.         syscolumns c,
  4303.         master.dbo.spt_datatype_info d,
  4304.         systypes t,
  4305.         syscolumns c2    /* Self-join to generate list of index columns and */
  4306.                         /* to extract datatype names */
  4307.     WHERE
  4308.         x.id = @table_id
  4309.         AND c2.name = INDEX_COL(@full_table_name,@indid,c.colid)
  4310.         AND c2.id =x.id
  4311.         AND c.id = x.id
  4312.         AND c.colid < keycnt+(x.status&16)/16
  4313.         AND x.indid = @indid
  4314.         AND c2.type = d.ss_dtype
  4315.         AND c2.usertype *= t.usertype
  4316.  
  4317. go
  4318.  
  4319. if    (charindex('6.00', @@version) = 0)
  4320. begin
  4321.     print ''
  4322.     print ''
  4323.     print 'Warning:'
  4324.     print 'you are installing the stored procedures '
  4325.     print 'on a pre 6.0 SQL Server.'
  4326.     print 'Ignore the following errors.'
  4327. end
  4328. else
  4329.     drop proc sp_special_columns
  4330. go
  4331.  
  4332. /*    Procedure for 6.0 server */
  4333. CREATE PROCEDURE sp_special_columns (
  4334.                  @table_name        varchar(32),
  4335.                  @table_owner        varchar(32) = null,
  4336.                  @table_qualifier    varchar(32) = null,
  4337.                  @col_type            char(1) = 'R',
  4338.                  @scope                char(1) = 'T',
  4339.                  @nullable            char(1) = 'U')
  4340. AS
  4341.     DECLARE @indid                int
  4342.     DECLARE @table_id            int
  4343.     DECLARE @full_table_name    char(70)
  4344.     DECLARE @msg                char(70)
  4345.     DECLARE @scopeout            smallint
  4346.  
  4347.     if @col_type not in ('R','V')
  4348.         begin
  4349.             raiserror (15251,-1,-1,'col_type','''R'' or ''V''')
  4350.             return
  4351.         end
  4352.  
  4353.     if @scope = 'C'
  4354.         select @scopeout = 0
  4355.     else if @scope = 'T'
  4356.         select @scopeout = 1
  4357.     else
  4358.         begin
  4359.             raiserror (15251,-1,-1,'scope','''C'' or ''T''')
  4360.             return
  4361.         end
  4362.  
  4363.     if @nullable not in ('U','O')
  4364.         begin
  4365.             raiserror (15251,-1,-1,'nullable','''U'' or ''O''')
  4366.             return
  4367.         end
  4368.  
  4369.     if @table_qualifier is not null
  4370.        begin
  4371.           if db_name() != @table_qualifier
  4372.               begin    /* If qualifier doesn't match current database */
  4373.                 raiserror (15250, -1,-1,'Table')
  4374.                 return
  4375.               end
  4376.        end
  4377.     if @table_owner is null
  4378.        begin    /* If unqualified table name */
  4379.           SELECT @full_table_name = @table_name
  4380.        end
  4381.     else
  4382.        begin    /* Qualified table name */
  4383.           SELECT @full_table_name = @table_owner + '.' + @table_name
  4384.        end
  4385.     /*    Get Object ID */
  4386.     SELECT @table_id = object_id(@full_table_name)
  4387.  
  4388.     if @col_type = 'V'
  4389.     BEGIN /* if ROWVER, just run that query */
  4390.         SELECT
  4391.             scope = convert(smallint,NULL),
  4392.             column_name = c.name,
  4393.             data_type = convert(smallint, -2),
  4394.             type_name = t.name,
  4395.             "precision" = convert(int,8),
  4396.             length = convert(int,8),
  4397.             scale = convert(smallint, NULL),
  4398.             pseudo_column = convert(smallint,1)
  4399.         FROM
  4400.             systypes t, syscolumns c, master.dbo.spt_datatype_info d
  4401.         WHERE
  4402.             c.id = @table_id
  4403.             AND c.type = d.ss_dtype
  4404.             AND c.usertype = 80 /*    TIMESTAMP */
  4405.             AND t.usertype = 80 /*    TIMESTAMP */
  4406.         RETURN
  4407.     END
  4408.  
  4409.     /* ROWID, now find the id of the 'best' index for this table */
  4410.  
  4411.     IF @nullable = 'O'    /* Don't include any indexes that contain
  4412.                            nullable columns. */
  4413.  
  4414.             SELECT @indid = MIN(indid)
  4415.                 FROM sysindexes i,syscolumns c,syscolumns c2
  4416.                 WHERE
  4417.                     i.status&2 = 2        /*    If Unique Index */
  4418.                      AND c.id = i.id
  4419.                      AND c2.id = c.id
  4420.                      AND c2.colid < i.keycnt + (i.status&16)/16
  4421.                     AND i.id = @table_id
  4422.                     AND indid > 0        /*    Eliminate Table Row */
  4423.                     AND c.name = index_col(@table_name,i.indid,c2.colid)
  4424.                     GROUP BY indid HAVING SUM(c.status&8) = 0
  4425.  
  4426.     ELSE    /* Include indexes that are partially nullable. */
  4427.  
  4428.         SELECT @indid = MIN(indid)
  4429.             FROM sysindexes i
  4430.             WHERE
  4431.                 status&2 = 2        /*    If Unique Index */
  4432.                 AND id = @table_id
  4433.                 AND indid > 0        /*    Eliminate Table Row */
  4434.  
  4435.     SELECT    /* INTn, FLOATn, DATETIMEn and MONEYn types */
  4436.         scope = @scopeout,
  4437.         column_name = INDEX_COL(@full_table_name,indid,c2.colid),
  4438.         data_type = d.data_type+convert(smallint,
  4439.                         /*                " I   I I FFMMDD" */
  4440.                         /*                " 1   2 4 484848" */
  4441.                         ascii(substring('666AAA@@@CB??GG',
  4442.                         2*(d.ss_dtype%35+1)+2-8/c.length,1))
  4443.                         -60),
  4444.         type_name = rtrim(substring(d.type_name,
  4445.                         1+
  4446.                         /*                " I   I I FFMMDD" */
  4447.                         /*                " 1   2 4 484848" */
  4448.                         ascii(substring('III<<<MMMI<<A<A',
  4449.                         2*(d.ss_dtype%35+1)+2-8/c.length,
  4450.                         1))-60, 13)),
  4451.         "precision" = convert(int,c.prec),
  4452.         length = d.length + convert(int,
  4453.                         /*                " I   I I FFMMDD" */
  4454.                         /*                " 1   2 4 484848" */
  4455.                         ascii(substring('AAA<BB<DDDHLUPP',
  4456.                         2*(d.ss_dtype%35+1)+2-8/c.length,
  4457.                         1))-64),
  4458.         scale = convert(smallint, c.scale),
  4459.         pseudo_column = convert(smallint,1)
  4460.     FROM
  4461.         sysindexes x,
  4462.         syscolumns c,
  4463.         master.dbo.spt_datatype_info d,
  4464.         systypes t,
  4465.         syscolumns c2    /* Self-join to generate list of index columns and */
  4466.                         /* to extract datatype names */
  4467.     WHERE
  4468.         x.id = @table_id
  4469.         AND c.name = INDEX_COL(@full_table_name,@indid,c2.colid)
  4470.         AND c.id = x.id
  4471.         AND c2.id = x.id
  4472.         AND c2.colid < x.keycnt+(x.status&16)/16
  4473.         AND x.indid = @indid
  4474.         AND c.type = d.ss_dtype
  4475.         AND c.usertype = t.usertype
  4476.         AND d.ss_dtype IN (111, 109, 38, 110)    /* Just *N types */
  4477.         AND c.usertype < 100                    /* No user defined types */
  4478.     UNION ALL
  4479.     SELECT    /* Identity base and decimal/numeric types */
  4480.         scope = @scopeout,
  4481.         column_name = INDEX_COL(@full_table_name,indid,c2.colid),
  4482.         data_type = convert(smallint,
  4483.                 /*    Map systypes.type to ODBC type */
  4484.                 /*        SS-Type         "     1 1             1           "*/
  4485.                 /*                        "33 3030    4 44 5 5 255 556666"*/
  4486.                 /*                        "45 7698    5 78 0 2 256 890123"*/
  4487.                         ascii(substring('8;<9?H><<<<:<=6<5<A<??@<GC?GB>',
  4488.                         t.type%34+1,1))-60),
  4489.         type_name = d.type_name,
  4490.         "precision" = convert(int,c.prec),
  4491.         length = convert(int,c.length) +
  4492.                 (convert(int,(c.type & 2)/2) |
  4493.                 (convert(int,c.type&64)/64))*(c.prec+2 - c.length),
  4494.         scale = convert(smallint, c.scale),
  4495.         pseudo_column = convert(smallint,1)
  4496.     FROM
  4497.         sysindexes x,
  4498.         syscolumns c,
  4499.         master.dbo.spt_datatype_info d,
  4500.         systypes t,
  4501.         syscolumns c2    /* Self-join to generate list of index columns and */
  4502.                         /* to extract datatype names */
  4503.     WHERE
  4504.         x.id = @table_id
  4505.         AND c.name = INDEX_COL(@full_table_name,@indid,c2.colid)
  4506.         AND c.id = x.id
  4507.         AND c2.id = x.id
  4508.         AND c2.colid < x.keycnt+(x.status&16)/16
  4509.         AND x.indid = @indid
  4510.         AND c.type = d.ss_dtype
  4511.         AND d.auto_increment = (c.status&128)/128
  4512.         AND c.usertype = t.usertype
  4513.         AND ((c.usertype < 100            /* Only base types */
  4514.             AND c.status&128 = 128)        /* Identity types only */
  4515.             OR d.ss_dtype IN (106, 108, 55, 63))     /* decimal/numeric types */
  4516.     UNION ALL
  4517.     SELECT    /* All other types including user data types */
  4518.         scope = @scopeout,
  4519.         column_name = INDEX_COL(@full_table_name,indid,c2.colid),
  4520.         data_type = convert(smallint,
  4521.                 /*    Map systypes.type to ODBC type */
  4522.                 /*        SS-Type         "     1 1             1           "*/
  4523.                 /*                        "33 3030    4 44 5 5 255 556666"*/
  4524.                 /*                        "45 7698    5 78 0 2 256 890123"*/
  4525.                         ascii(substring('8;<9?H><<<<:<=6<5<A<??@<GC?GB>',
  4526.                         t.type%34+1,1))-60),
  4527.         type_name = t.name,
  4528.         "precision" = isnull(convert(int,c.prec), 2147483647),
  4529.         length = isnull(d.length, convert(int,c.length)) +convert(int,
  4530.                         isnull(d.aux,
  4531.                         /*                " I   I I FFMMDD" */
  4532.                         /*                " 1   2 4 484848" */
  4533.                         ascii(substring('AAA<BB<DDDHLUPP',
  4534.                         2*(d.ss_dtype%35+1)+2-8/c.length,
  4535.                         1))-64)),
  4536.         scale = convert(smallint, c.scale),
  4537.         pseudo_column = convert(smallint,1)
  4538.     FROM
  4539.         sysindexes x,
  4540.         syscolumns c,
  4541.         master.dbo.spt_datatype_info d,
  4542.         systypes t,
  4543.         syscolumns c2    /* Self-join to generate list of index columns and */
  4544.                         /* to extract datatype names */
  4545.     WHERE
  4546.         x.id = @table_id
  4547.         AND c.name = INDEX_COL(@full_table_name,@indid,c2.colid)
  4548.         AND c.id = x.id
  4549.         AND c2.id = x.id
  4550.         AND c2.colid < x.keycnt+(x.status&16)/16
  4551.         AND x.indid = @indid
  4552.         AND c.type = d.ss_dtype
  4553.         AND isnull(d.auto_increment,0) = 0
  4554.         AND (c.status&128 = 0            /* No Identity types unless */
  4555.             OR c.usertype >= 100)        /* User defined type */
  4556.         AND c.usertype *= t.usertype
  4557.         AND (d.ss_dtype NOT IN (111, 109, 38, 110)    /* No *N types */
  4558.             OR c.usertype >= 100)                    /* User defined types */
  4559.         AND d.ss_dtype NOT IN (106, 108, 55, 63)    /* No decimal/numeric types */
  4560. go
  4561.  
  4562. grant execute on sp_special_columns to public
  4563. go
  4564.  
  4565. print 'creating sp_databases'
  4566. go
  4567.  
  4568. create proc sp_databases
  4569. as
  4570.     /* Use temporary table to sum up database size w/o using group by */
  4571.     create table #databases (
  4572.                   database_name varchar(32) NOT NULL,
  4573.                   size int NOT NULL)
  4574.  
  4575.     /* Insert row for each database */
  4576.     insert into #databases
  4577.         select
  4578.             name,
  4579.             (select sum(size) from master.dbo.sysusages
  4580.                 where dbid = d.dbid)
  4581.         from master.dbo.sysdatabases d
  4582.  
  4583.     select
  4584.          database_name,
  4585.          database_size = size*2,    /* Convert from 2048 byte pages to K */
  4586.          remarks = convert(varchar(254),null)    /* Remarks are NULL */
  4587.     from #databases
  4588.     order by database_name
  4589. go
  4590.  
  4591. grant execute on sp_databases to public
  4592. go
  4593.  
  4594. if exists (select * from sysobjects where name = 'sp_configure'
  4595.             and sysstat & 0xf = 4)
  4596.     begin
  4597.         exec sp_configure 'allow updates',0
  4598.         reconfigure with override
  4599.     end
  4600. go
  4601.  
  4602. if exists (select * from sysobjects where name = 'sp_check_objects'
  4603.             and sysstat & 0xf = 4)
  4604.     begin
  4605.         /* Only supported on 6.0 servers */
  4606.         print ''
  4607.         print 'Checking objects created by instcat.sql.'
  4608.  
  4609.         exec sp_check_objects 'catalog'
  4610.     end
  4611. go
  4612.  
  4613. print ''
  4614. print 'inscat.sql completed successfully.'
  4615. go
  4616.  
  4617. dump tran master with no_log
  4618. go
  4619. checkpoint
  4620. go
  4621.